gpt4 book ai didi

python - selenium.common.exceptions.ElementNotVisibleException : Message: element not interactable and explicit wait not working with Selenium and Python

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:28 25 4
gpt4 key购买 nike

我正在尝试访问 PenFed为了得到我目前的未付金额。我做了很多研究,不幸的是,我仍然感到困惑。我正在使用 Python Selenium,我试图单击侧面的初始登录按钮以查看用户名字段。这是元素的 HTML 代码:

<a href="https://www.penfed.org/" class="pfui-button-login login-slide-button pfui-button pfui-btn-tertiary-dark-blue-outline" id="mobile-login" data-di-id="#mobile-login">Login</a>

当我尝试运行以下代码时:

driver.find_element_by_id("mobile-login").click()

我收到以下错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

即使我尝试使用 WebDriver Wait 函数,例如这些:

try:
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "mobile-login"))).click()
except ElementNotVisibleException:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, "mobile-login"))).click()

无论我让他们等多久,我都会收到一条超时消息:

raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

我的所有研究都表明调用等待函数应该可以解决这个问题,但它对我不起作用。我还读到在单击按钮之前我必须调用的元素顶部可能有一个图像覆盖,但我也没有在网站代码中看到任何内容。如果我正在测试它,我能够通过代码点击按钮的唯一方法是我首先点击它,所以我不知道我可以使用的任何其他东西。预先感谢您的帮助!

更新:我发现以下代码对我有用:

element = driver.find_element_by_id("mobile-login")
driver.execute_script("$(arguments[0]).click();", element)

但我不知道 execute_script 实际上做了什么。有人可以解释那段代码是否有效,或者是否有任何其他替代方案适用于他们?

最佳答案

您指定的代码是JQuery . execute_script(p1, p2) 运行一个 js 脚本,其中 p1 是脚本(在您的例子中是单击元素的 JQuery 行),p2 是所需的元素。如果 arguments[0] 等于“element”,您似乎不需要 p2,但我不完全确定。

一个可能的解决方法是使用一个计数器来记录您点击该元素的次数。如果计数器达到一定数量并且页面没有变化(您可以通过在当前页面上查找唯一元素/值来检查),那么您就知道它不可点击。

祝你好运!

关于python - selenium.common.exceptions.ElementNotVisibleException : Message: element not interactable and explicit wait not working with Selenium and Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54504453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com