python - Chrome webdriver(Selenium)在获取特定元素后停止执行
问题描述
在 Selenium 中使用 chromeDriver (84.0.4147.30)(使用 Python),程序在获取特定元素时停止并等待用户操作(例如,按 ALT 键),保持执行就像没有时间过去一样(WebDriverWait 没有t 独立于经过的时间触发时间异常)。
我已经更改了浏览器功能(['pageLoadStrategy'] = 'eager'),Expected_Conditions (element_to_be_clickable和presence_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
解决方案
推荐阅读
- python - 如何从 App Engine 将图像上传到 Google Cloud Storage
- rust - Rust SDL:如何将表面“附加”到窗口?
- c++ - C++ 程序在重新执行时加速
- python - 已解决——SQL 用序列号更新字段
- r - 将一系列条件函数应用于 data.table
- firebase - 如果您让任何人读取数据但不写入,Firestore 的安全性如何
- python - “字节”对象没有属性“格式”
- swift - 对象在 SwiftUI 工作表中未对齐
- python - 熊猫数据框到 html,无法显示到小数点后 2 位
- python - 我的程序正在运行,但已最小化