【发布时间】: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
【问题讨论】: