ElementNotInteractableException:消息:尝试使用Selenium和Python单击元素时,元素无法滚动到视图中 [英] ElementNotInteractableException: Message: Element could not be scrolled into view while trying to click an element using Selenium and Python

查看:609
本文介绍了ElementNotInteractableException:消息:尝试使用Selenium和Python单击元素时,元素无法滚动到视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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次有效,但经常会因此错误消息而失败

as you can see I first switch window and then check for an element, get that element (a button) and finally try to click on said button. This works maybe 2 out of 3 times but ever so often does it fail with this error message

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

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

When visually looking at the flow when it is executing everything seems fine (my first guess was that the window switch didn't work as expected) and the browser ends up in the expected state where I am able to manually click this button. Interestingly enough, there is no timeout or similar when this error occurs, it happens instantly during execution.

有什么想法吗?

推荐答案

当您要单击的元素出现在页面上但不完全可见并且硒试图单击的点不存在时,通常会出现此问题可见.
在这种情况下,您可以使用javascript单击元素,该元素实际上直接在页面的html结构上运行.
您可以像这样使用它:

This problem usually arises when the element you are trying to click is present on the page but it is not fully visible and the point where selenium tries to click is not visible.
In this case, you can use javascript to click on the element, which actually operates directly on the html structure of the page.
You can use it like:

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

这篇关于ElementNotInteractableException:消息:尝试使用Selenium和Python单击元素时,元素无法滚动到视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆