使用Undetected-chromedriver时出现shared_storage_worklet错误求助
问题:使用Undetected-chromedriver切换窗口时触发DevTools错误
我编写了一个网页抓取脚本,因目标网站采用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 = r"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和undetected-chromedriver库,但问题仍未解决,请问我哪里操作有误?
解决方案
1. 移除重复的新窗口创建操作
你同时使用ActionChains模拟Ctrl+T打开新标签和driver.switch_to.new_window(),两个操作会导致浏览器窗口句柄混乱,触发DevTools未知类型错误。只需保留一种方式,推荐用driver.switch_to.new_window(),原生API更稳定。
2. 修复元素等待逻辑
原代码每次等待第一个元素,但循环要获取第a个元素,可能出现第一个元素加载完成但目标元素未渲染的情况。修改等待条件为当前循环目标元素的XPATH。
3. 处理异常流程
如果try块获取link失败,后续driver.get(link)会报错,需在except中添加continue跳过当前循环。
4. 简化窗口切换代码
切换回父窗口时直接用parent_window_handle变量即可,无需转字符串,它本身就是字符串类型的窗口句柄。
修改后的完整代码
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import undetected_chromedriver as uc options = uc.ChromeOptions() user_data_dir = r"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: a += 1 try: # 等待当前索引的元素加载完成 target_xpath = f"//ul/div[{a}]/li/a" element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, target_xpath)) ) link = element.get_attribute("href") except Exception as e: print(f"获取第{a}个列表失败: {e}") continue # 打开新窗口并访问链接 driver.switch_to.new_window() driver.get(link) # 在这里添加数据抓取逻辑 # scrape data beep boop ........ # # 关闭当前窗口并切回父窗口 driver.close() driver.switch_to.window(parent_window_handle) driver.quit()
内容的提问来源于stack exchange,提问作者Macrowave
相关产品推荐
相关产品推荐

