首页 > 解决方案 > Chrome webdriver(Selenium)在获取特定元素后停止执行

问题描述

在 Selenium 中使用 chromeDriver (84.0.4147.30)(使用 Python),程序在获取特定元素时停止并等待用户操作(例如,按 ALT 键),保持执行就像没有时间过去一样(WebDriverWait 没有t 独立于经过的时间触发时间异常)。

我已经更改了浏览器功能(['pageLoadStrategy'] = 'eager'),Expected_Conditions (element_to_be_clickablepresence_of_element_located)但没有成功。我只需要点击一个元素。此时对其进行的任何获取都会冻结。

很难为您提供一个最小的工作示例,因为所有这些都发生在特定且受限制的网页中(需要登录并且只能由管理员创建帐户)。但是下面给出了我的脚本的初始配置和有问题的功能。

from selenium.webdriver import Chrome as Driver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
#... more imports*

def strange_correction(): #something useful after login page (without this, the execution freezes) o.O
   browser.refresh()
#... more definitions

pathdriver = "path/to/webdriver"
webmainsource = "https://adress"


TIMEOUT = 20    

nav_options= Options()
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['pageLoadStrategy'] = 'eager'

browser = Driver(options=nav_options, executable_path=pathdriver, desired_capabilities=capabilities)
browser.maximize_window()
browser.get(webmainsource)

###### Problematic Function (maybe) ######
def wait_for_xpelement(xpath): 

   WebDriverWait(browser, TIMEOUT).until( EC.element_to_be_clickable(( BY.XPATH, xpath )) )
   return WebDriverWait(browser, TIMEOUT).until( EC.presence_of_element_located(( BY.XPATH, xpath )) )

自然地,我改变了wait_for_xpelement(xpath)函数,有时更简单或更复杂。睡眠功能也不起作用。无论如何,我该如何处理这个问题?

附加信息:Debian 9、Python 3.8

标签: pythonselenium-chromedriverfreezewebdriverwait

解决方案


推荐阅读