Python-Selenium:Element当前不可见,可能无法操作
我试图找出出现以下错误的原因:Python-Selenium:Element当前不可见,可能无法操作,python,selenium,phantomjs,Python,Selenium,Phantomjs,我试图找出出现以下错误的原因: Element is not currently visible and may not be manipulated 我想找到next按钮并单击它 这是网页: 我的驱动程序类中有此代码: def click_next(self): try: self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") self.driver.f
Element is not currently visible and may not be manipulated
我想找到next
按钮并单击它
这是网页:
我的驱动程序
类中有此代码:
def click_next(self):
try:
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
self.driver.find_element_by_xpath('//li[@class="paging-item"]/a').click()
except:
raise
return False
你知道问题出在哪里吗?你能试试下面的xpath吗?xpath实际上返回4个匹配项
"//li[@class='paging-item']//a[@class='btn-paging-pn icof icon-arr-right paging-next']"
XPath查询返回4个元素。您必须知道,在这种情况下,Selenium默认使用第一个元素。如果这样的元素不可见,那么您就不能使用click与之交互。为什么要使用
xpath
来搜索类名,而有一个方法正好可以这样做,find\u element\u by\u class\u name
PhantomJS
的行为常常很怪异。很多时候,当元素当前不可见且可能无法操作时,这是因为窗口大小。尝试重新调整窗口大小,这对我来说很有用(在ubuntu14.04
上用phantomjs1.9.8
测试)
查看元素的位置。假设它有负数。滚动窗口,使元素位于可见区域
我也有同样的问题,元素的位置属性是(807,-30)。下面的代码修复了此问题
driver.get(url)
driver.execute_script("window.scrollTo(0, 60);")
element = findElement(driver, "//button[contains(text(),'some text')]", WAIT_IN_SEC)
element.click()
driver.get(url)
driver.execute_script("window.scrollTo(0, 60);")
element = findElement(driver, "//button[contains(text(),'some text')]", WAIT_IN_SEC)
element.click()