[英]How to click until next page is noninteractable in Selenium python?
When scrolling a certain page which 'next page' button, I want to keep clicking till all contents are shown.当滚动“下一页”按钮的某个页面时,我想一直点击直到显示所有内容。 However the issue with this site is that at the end the button doesn't disappear, it can be found but in a nonInteractable state, if clicking on it, the page will return error.
然而,这个网站的问题是最后按钮并没有消失,它可以被找到,但处于不可交互状态,如果点击它,页面将返回错误。 How should I stop clicking before the exception?
我应该如何在异常之前停止点击? The code below will make the page return error as it'd click on the last 'nonInteractable' button.
下面的代码将使页面返回错误,因为它会点击最后一个“不可交互”按钮。
next_button=True
while next_button:
try:
next_button = driver.find_element_by_class_name('next_page')
next_button.click()
except ElementNotInteractableException:
next_button=False
break
You can make a user defined function like this:您可以像这样创建用户定义的函数:
def elementPresent(locatorType, locator):
#present = true
#not present = false
wait = WebDriverWait(driver, 20)
try:
wait.until(EC.presence_of_element_located((locatorType, locator)))
wait.until(EC.visibility_of_element_located((locatorType, locator)))
except Exception:
return False
return True
This function basically is checking the presence and visibility of element.这个功能基本上是检查元素的存在和可见性。 If element with given locator is found, function returns True otherwise False
如果找到具有给定定位器的元素,则函数返回 True 否则返回 False
To call the function, you can use looping, like while loop要调用该函数,您可以使用循环,例如 while 循环
For eg :例如:
while(locatorType, locator):
element = driver.find_element_by_class_name('next_page')
element.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.