9
9
import undetected_chromedriver as uc
10
10
import os
11
11
from urllib .parse import urlparse
12
+ import os
13
+ import re
14
+
15
+ def add_capsolver_api_key (file_path , api_key ):
16
+ with open (file_path , 'r' ) as file :
17
+ content = file .read ()
18
+
19
+ updated_content = re .sub (r'apiKey:\s*\'[^\']*\'' , f'apiKey: \' { api_key } \' ' , content )
12
20
21
+ with open (file_path , 'w' ) as file :
22
+ file .write (updated_content )
13
23
14
- def create_driver (browser , captcha_extension = False , proxy = None ):
24
+ def create_driver (browser , captcha_extension = False , proxy = None , captcha_key = None ):
15
25
"""
16
26
Create a WebDriver instance for the specified browser with optional configurations.
17
27
@@ -40,7 +50,7 @@ def create_driver(browser, captcha_extension=False, proxy=None):
40
50
options = FirefoxOptions ()
41
51
options .add_argument ('--no-sandbox' )
42
52
options .add_argument ('--disable-gpu' )
43
- options .add_argument ('--headless' )
53
+ # options.add_argument('--headless')
44
54
custom_profile .set_preference ("intl.accept_languages" , "en-us" )
45
55
46
56
# proxy
@@ -67,12 +77,14 @@ def create_driver(browser, captcha_extension=False, proxy=None):
67
77
options .add_argument ('--no-sandbox' )
68
78
options .add_argument ('--disable-gpu' )
69
79
options .add_experimental_option ('prefs' , {'intl.accept_languages' : 'en-us' })
70
- options .add_argument ('--headless=new' )
80
+ # options.add_argument('--headless=new')
71
81
options .add_experimental_option ('excludeSwitches' , ['enable-logging' ])
72
82
if proxy :
73
83
options .add_argument (f'--proxy-server={ proxy } ' )
74
84
if captcha_extension :
75
- options .add_extension (os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'captcha_solvers/capsolver_captcha_solver-1.10.4.crx' ))
85
+ add_capsolver_api_key (os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'captcha_solvers/capsolver-chrome-extension/assets/config.js' ), captcha_key )
86
+ ext_path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'captcha_solvers/capsolver-chrome-extension/' )
87
+ options .add_argument (f'--load-extension={ ext_path } ' )
76
88
77
89
driver = webdriver .Chrome (service = ChromeService (ChromeDriverManager ().install ()), options = options )
78
90
elif browser == 'undetected-chrome' :
@@ -84,10 +96,11 @@ def create_driver(browser, captcha_extension=False, proxy=None):
84
96
if proxy :
85
97
options .add_argument (f'--proxy-server={ proxy } ' )
86
98
if captcha_extension :
99
+ add_capsolver_api_key (os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'captcha_solvers/capsolver-chrome-extension/assets/config.js' ), captcha_key )
87
100
ext_path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'captcha_solvers/capsolver-chrome-extension/' )
88
101
options .add_argument (f'--load-extension={ ext_path } ' )
89
102
90
- driver = uc .Chrome (options = options , headless = True , use_subprocess = False )
103
+ driver = uc .Chrome (options = options , headless = False , use_subprocess = False )
91
104
else :
92
105
raise ValueError ('Unsupported browser' )
93
106
return driver
0 commit comments