【问题标题】:ElementNotInteractableException: Message: Element could not be scrolled into view while trying to click an element using Selenium and PythonElementNotInteractableException:消息:尝试使用 Selenium 和 Python 单击元素时无法将元素滚动到视图中
【发布时间】:2019-08-09 06:01:42
【问题描述】:

我有这个代码:

driver.switch_to.window(window_after)

try:
    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.NAME, '_eventId_confirmed')))
    print ("Page 2 is ready!")
except TimeoutException:
    print ("Loading took too much time!")

btn = driver.find_element_by_name('_eventId_confirmed')

btn.click()

如您所见,我首先切换窗口,然后检查一个元素,获取该元素(一个按钮),最后尝试单击所述按钮。这可能工作 3 次中的 2 次,但它经常失败并显示此错误消息

selenium.common.exceptions.ElementNotInteractableException: Message: Element <button class="btn" name="_eventId_confirmed"> could not be scrolled into view

在执行流程时,在视觉上查看流程似乎一切正常(我的第一个猜测是窗口开关没有按预期工作)并且浏览器最终处于预期状态,我可以手动单击此按钮.有趣的是,发生此错误时没有超时或类似情况,它在执行过程中立即发生。

有什么想法吗?

【问题讨论】:

  • 你试过javascript scrollIntoView选项吗?点击前driver.execute_script("arguments[0].scrollIntoView;",btn)
  • 抱歉错过了()。这是正确的代码。 driver.execute_script("arguments[0].scrollIntoView();",btn)
  • 已经添加了答案,如果有帮助请告诉我

标签: python selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

当您尝试单击的元素出现在页面上但它不完全可见并且 selenium 尝试单击的点不可见时,通常会出现此问题。
在这种情况下,可以使用javascript点击元素,实际上是直接对页面的html结构进行操作。
你可以像这样使用它:

element = driver.find_element_by_name("_eventId_confirmed")
driver.execute_script("arguments[0].click();", element)

【讨论】:

    【解决方案2】:

    这里有两个选项。

    使用 selenium location_once_scrolled_into_view 方法:

    btn.location_once_scrolled_into_view
    

    使用 Javascript:

    driver.execute_script("arguments[0].scrollIntoView();",btn)
    

    示例代码:

    url = "https://stackoverflow.com/questions/55228646/python-selenium-cant-sometimes-scroll-element-into-view/55228932?    noredirect=1#comment97192621_55228932"
    driver.get(url)
    element = driver.find_element_by_xpath("//a[.='Contact Us']")
    element.location_once_scrolled_into_view
    time.sleep(1)
    driver.find_element_by_xpath("//p[.='active']").location_once_scrolled_into_view
    driver.execute_script("arguments[0].scrollIntoView();",element)
    

    【讨论】:

    • 谢谢。我想我理解 JS sn-p 但 Selenium 的例子听起来很违反直觉?
    • 嗯,IntelliJ/PyCharm 显示警告Statement seems to have no effect less... (⌘F1) Inspection info: This inspection detects statements without any effect.?
    • 不确定您收到该消息的原因。我能够毫无问题地运行示例脚本。在上面的答案中添加了脚本供您参考。
    • 我强烈推荐第二种解决方案。第一个见source code。一方面,描述该方法的评论的第一句话是:“此属性可能会在没有警告的情况下更改。”他们没有提供比这更多的细节,我不确定他们到底指的是什么变化,但这听起来不太好。其次,该函数所做的只是执行它自己的(更复杂的)Javascript。
    【解决方案3】:

    由于您的最后一步是在所需元素上调用click(),因此您需要使用element_to_be_clickable() 而不是使用expected_conditions 作为presence_of_element_located(),如下所示:

    try:
        myElem = WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.NAME, '_eventId_confirmed')))
    

    【讨论】:

    • 只是一个疑问,当打开页面后元素不可点击时,条件element_to_be_clickable有意义吗?因为如果我们不将其滚动到视图中,它将永远无法点击,并且条件 element_to_be_clickable 可能会给出超时异常。
    • 嗯,element_to_be_clickable() 默认滚动有一定的限制。让我知道执行状态。
    • 无法检查 OP 网站,但我过去也遇到过 ElementNotClickable 异常,我使用 javascript click 解决了它。不太确定硒等待元素可点击是否会滚动元素。
    • @SameerArora OP 正在使用 Python 客户端,因此直接跳到使用 javascript click() 将违反所有最佳实践。您需要正确探索expected_conditions。如果您不太确定 selenium 等待元素可点击是否会滚动元素,您需要再次正确阅读文档。
    • 是的,现在研究了代码,方法中没有涉及到滚动。该方法及其后续方法正在使用 isDisplayed() 检查其可见性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2019-08-08
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多