Python Selenium脚本崩溃,报Element Not Found错误求助
问题:Python Selenium机器人打开第一个链接后崩溃,报元素找不到错误
我正在开发一个Python机器人,运行代码时能正确打开列表中的第一个链接,但随后崩溃,出现Element Not Found错误。我是Python新手,不知道该如何解决。
代码示例
options = Options() options.add_experimental_option("detach", True) chromedriver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) chromedriver.get("https://www.ebay.co.uk/usr/usernamehere") links = chromedriver.find_elements("xpath", "//ol[@class='pagination__items']//a[@href]") # Create a list to store the opened window handles window_handles = [] for link in links: url = link.get_attribute("href") print(url) # Open the link in a new tab chromedriver.execute_script("window.open('"+url+"', '_blank');") # Switch to the newly opened tab chromedriver.switch_to.window(chromedriver.window_handles[-1]) # Wait for an element on the page to become visible element = WebDriverWait(chromedriver, 10).until( EC.presence_of_element_located((By.XPATH, "//some_xpath_selector")) ) # Perform your actions on the page here # For example, you can get the page title: print(chromedriver.title) # Add the window handle to the list window_handles.append(chromedriver.current_window_handle) # Switch back to the first tab chromedriver.switch_to.window(chromedriver.window_handles[0]) # Close all the other tabs for handle in window_handles[1:]: chromedriver.switch_to.window(handle) chromedriver.close() # Switch back to the first tab and quit the browser chromedriver.switch_to.window(chromedriver.window_handles[0]) chromedriver.quit()
错误日志
[20612:16724:0323/231628.565:ERROR:device_event_log_impl.cc(222)] [23:16:28.565] USB: usb_service_win.cc:104 SetupDiGetDeviceProperty({{A45C254E-DF1C-4EFD-8020-67D146A850E0}, 6}) failed: Element not found. (0x490) [20612:16724:0323/231628.566:ERROR:device_event_log_impl.cc(222)] [23:16:28.565] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2) [20612:16724:0323/231628.566:ERROR:device_event_log_impl.cc(222)] [23:16:28.566] USB: usb_service_win.cc:104 SetupDiGetDeviceProperty({{A45C254E-DF1C-4EFD-8020-67D146A850E0}, 6}) failed: Element not found. (0x490) [20612:16724:0323/231628.580:ERROR:device_event_log_impl.cc(222)] [23:16:28.580] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F) [20612:16724:0323/231628.599:ERROR:device_event_log_impl.cc(222)] [23:16:28.598] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
解决办法
1. 区分错误类型
你看到的USB相关错误是ChromeDriver与系统USB设备的兼容性问题,不会导致代码崩溃,真正让代码终止的是WebDriverWait等待的元素定位失败(也就是代码中的//some_xpath_selector对应的元素不存在)。
2. 修复元素定位问题
- 替换
//some_xpath_selector为目标页面真实存在的元素XPath:打开Chrome开发者工具(F12),找到目标元素右键选择「复制」→「复制XPath」。 - 如果元素是动态加载的,建议改用
EC.visibility_of_element_located代替EC.presence_of_element_located,前者确保元素可见,后者仅确认元素存在于DOM中。
3. 优化循环逻辑,避免DOM引用失效
循环中打开新标签后,原页面的links元素列表可能因为DOM变化失效,建议先提取所有链接URL再循环处理:
# 先提取所有链接URL,避免后续DOM变化导致元素引用丢失 links = chromedriver.find_elements("xpath", "//ol[@class='pagination__items']//a[@href]") url_list = [link.get_attribute("href") for link in links] # 循环处理每个URL for url in url_list: print(url) chromedriver.execute_script("window.open('"+url+"', '_blank');") chromedriver.switch_to.window(chromedriver.window_handles[-1]) # 等待元素,替换为真实的XPath try: element = WebDriverWait(chromedriver, 10).until( EC.visibility_of_element_located((By.XPATH, "//真实元素的XPath")) ) print(chromedriver.title) window_handles.append(chromedriver.current_window_handle) except TimeoutException: # 处理元素找不到的情况,比如跳过当前页面 print(f"页面 {url} 未找到目标元素,跳过") chromedriver.close() # 关闭当前失败的标签页 continue
4. 屏蔽USB错误日志(可选)
如果觉得USB日志干扰视线,可以在ChromeOptions中添加参数屏蔽:
options.add_experimental_option('excludeSwitches', ['enable-logging'])
内容的提问来源于stack exchange,提问作者Abdullah Ergun
相关产品推荐
相关产品推荐

