我为一些网页抓取应用程序编写了一个简单的脚本,因为该网站使用 Cloudflare 机器人检测,所以我需要使用 UnDetected-chromedriver。这是我的代码:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import undetected_chromedriver as uc
options = uc.ChromeOptions()
user_data_dir = "C:\\Users\\Stack\\Desktop\\Chrome Profile\\Profile"
driver = uc.Chrome(use_subprocess=True,user_data_dir=user_data_dir)
driver.maximize_window()
a = 0
driver.get("https://www.nhatot.com/mua-ban-bat-dong-san")
parent_window_handle = driver.current_window_handle
while a <= 26:
try:
a += 1
element_present = EC.presence_of_element_located((By.XPATH, "//ul/div[1]/li/a"))
WebDriverWait(driver, 10).until(element_present)
link = driver.find_element(By.XPATH, "//ul/div["+str(a)+"]/li/a").get_attribute("href")
except Exception as e:
print("No listing " +str(e))
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
driver.switch_to.new_window()
driver.get(link)
# scrape data beep boop ........ #
driver.close()
driver.switch_to.window(''+str(parent_window_handle))
运行代码时出现此错误:
Traceback (most recent call last):
File "C:\Users\Stack\IdeaProjects\Python Automation\Project\Profile\New Profile\test.py", line 25, in <module>
driver.switch_to.new_window()
File "C:\Users\Stack\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\switch_to.py", line 109, in new_window
self._w3c_window(value["handle"])
File "C:\Users\Stack\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\switch_to.py", line 141, in _w3c_window
send_handle(window_name)
File "C:\Users\Stack\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\switch_to.py", line 137, in send_handle
self._driver.execute(Command.SWITCH_TO_WINDOW, {"handle": h})
File "C:\Users\Stack\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute
self.error_handler.check_response(response)
File "C:\Users\Stack\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevTools returned unknown type:shared_storage_worklet
(Session info: chrome=122.0.6261.95)
Stacktrace:
GetHandleVerifier [0x009B8C93+51395]
(No symbol) [0x00925EF1]
(No symbol) [0x007DE13A]
(No symbol) [0x007EB131]
(No symbol) [0x007EB4C7]
(No symbol) [0x007DE6BC]
(No symbol) [0x007C22ED]
(No symbol) [0x0083F738]
(No symbol) [0x00833E53]
(No symbol) [0x0080C629]
(No symbol) [0x0080D40D]
GetHandleVerifier [0x00D36453+3711107]
GetHandleVerifier [0x00D7583A+3970154]
GetHandleVerifier [0x00D70B28+3950424]
GetHandleVerifier [0x00A69C99+776393]
(No symbol) [0x009316C4]
(No symbol) [0x0092C5A8]
(No symbol) [0x0092C759]
(No symbol) [0x0091DD80]
BaseThreadInitThunk [0x7731FA29+25]
RtlGetAppContainerNamedObjectPath [0x77467A9E+286]
RtlGetAppContainerNamedObjectPath [0x77467A6E+238]
Process finished with exit code 1
我尝试更新 selenium、未检测到的 chromedriver 库,但没有任何效果。我在这里做错了什么?
该问题在 Chrome 123 上仍然存在吗?