【问题标题】:Selenium element was not scrolled into the viewport when the element is "huge"当元素“巨大”时,Selenium 元素未滚动到视口中
【发布时间】:2015-06-20 11:28:00
【问题描述】:

我正在使用 Selenium Webdriver 和 Python 进行单元测试。当我尝试测试单击网页上的元素时,出现错误: IE:11,Windows-7

ElementNotVisibleException: Message: The point at which the driver is attempting to click on the element was not scrolled into the viewport.

即使我尝试在视图中垂直和水平移动元素。当我单击带有e.click() 的元素时,仍然存在错误。 我在网上搜了一下,找到了这个,把e.click()改成了webdriver.ActionChains(driver).move_to_element(e).click(e).perform()。现在它通过了,但实际上并没有点击,似乎只是跳过了这一行。

这是我的代码 sn-p:

e = elems.find_element_by_xpath("//*[@id='schedItem_10262']/div[2]") # find the sub-element from elems by xpath
self.scroll_element_into_view(e)
e.click()
#webdriver.ActionChains(driver).move_to_element(e).click(e).perform()

def scroll_element_into_view(self, element):
    """Scroll element into view"""
    x = element.location['x']
    self.driver.execute_script('window.scrollTo({0}, 0)'.format(x))
    y = element.location['y']
    self.driver.execute_script('window.scrollTo(0, {0})'.format(y))

当元素“巨大”而无法放入窗口时,就会出现问题。有什么方法可以点击一个“巨大”的元素?

我对此很陌生。欢迎任何建议。谢谢。

更新: 使用以下代码访问并点击:

e = driver.find_element_by_xpath("//*[@id='schedItem_10262']/div[2]")
self.scroll_element_into_view(e)
e.click()

这是一个快照,它是显示所有案例的时间线,这张图片中有两个案例。 “疝气”和“反式”。红色的“疝气”持续时间很长,所以在网页上没有完全显示。

我想一个一个地点击每个案例,看看每个案例是否有窗口弹出。当我尝试手动单击两种情况时,它们都可以正常工作。但是当我尝试用click()点击“Hernia”时会报错,element was not scrolled into the viewport

【问题讨论】:

    标签: python selenium webdriver


    【解决方案1】:

    在所需元素上调用scrollIntoView(),而不是scrollTo()

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

    【讨论】:

    • 感谢您的回答。我尝试了您的解决方案,但遇到了同样的错误:(它在相对较小的元素上工作得非常好。是因为硒无法处理“大”元素点击吗?我确定大元素的一部分是可见的,当我尝试手动点击它,它工作正常。
    • @Julia hm,感谢您的快速周转!你能分享一个可复制的例子吗? (会更容易提供帮助)
    • 对不起,我可以分享代码,但服务器不公开测试。我更新了我的帖子。请检查它是否有帮助。谢谢~~
    猜你喜欢
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多