使用未检测到的 chromedriver + selenium 打开第二个 window,python
opening the second window using undetected chromedriver + selenium, python
我正在尝试打开两个或多个单独的 windows。
我能够通过 运行
打开第一个 window
from selenium import webdriver
import undetected_chromedriver.v2 as uc
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\username\AppData\Local\Google\Chrome\User Data")
drivers = list()
drivers.append(uc.Chrome(options=options))
现在我试图通过简单地重复最后一行 (drivers.append(uc.Chrome(options=options))
) 打开第二个 window,但它返回
RuntimeError: you cannot reuse the ChromeOptions object
所以我尝试了
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\username\AppData\Local\Google\Chrome\User Data")
drivers.append(uc.Chrome(options=options))
这次回来了
WebDriverException: unknown error: cannot connect to chrome at 127.0.0.1:54208
from chrome not reachable
我该如何解决这个问题?
这对我有用,我不能使用 v2,但它适用于 v1。
import undetected_chromedriver as uc
uc.install(executable_path=PATH,)
drivers_dict={}
def scraping_function(link):
try:
thread_name= threading.current_thread().name
#sometime we are going to have different thread name in each iteration so a little regex might help
thread_name = re.sub("ThreadPoolExecutor-(\d*)_(\d*)", r"ThreadPoolExecutor-0_", thread_name)
print(f"re.sub -> {thread_name}")
driver = drivers_dict[thread_name]
except KeyError:
drivers_dict[threading.current_thread().name] = uc.Chrome(options=options,executable_path=PATH)
driver = drivers_dict[threading.current_thread().name]
driver.get(link)
我正在尝试打开两个或多个单独的 windows。
我能够通过 运行
打开第一个 windowfrom selenium import webdriver
import undetected_chromedriver.v2 as uc
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\username\AppData\Local\Google\Chrome\User Data")
drivers = list()
drivers.append(uc.Chrome(options=options))
现在我试图通过简单地重复最后一行 (drivers.append(uc.Chrome(options=options))
) 打开第二个 window,但它返回
RuntimeError: you cannot reuse the ChromeOptions object
所以我尝试了
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\username\AppData\Local\Google\Chrome\User Data")
drivers.append(uc.Chrome(options=options))
这次回来了
WebDriverException: unknown error: cannot connect to chrome at 127.0.0.1:54208
from chrome not reachable
我该如何解决这个问题?
这对我有用,我不能使用 v2,但它适用于 v1。
import undetected_chromedriver as uc
uc.install(executable_path=PATH,)
drivers_dict={}
def scraping_function(link):
try:
thread_name= threading.current_thread().name
#sometime we are going to have different thread name in each iteration so a little regex might help
thread_name = re.sub("ThreadPoolExecutor-(\d*)_(\d*)", r"ThreadPoolExecutor-0_", thread_name)
print(f"re.sub -> {thread_name}")
driver = drivers_dict[thread_name]
except KeyError:
drivers_dict[threading.current_thread().name] = uc.Chrome(options=options,executable_path=PATH)
driver = drivers_dict[threading.current_thread().name]
driver.get(link)