简体   繁体   English

如何在Selenium python中单击直到下一页不可交互?

[英]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.

相关问题 Selenium 点击下一页按钮直到消失 - Selenium click on next page button until disappears Selenium Python - 做动作,点击下一页,重复直到最后一页 - Selenium Python - Do action, click next page, repeat until last page Python中的Selenium:如果不存在链接,则不知道如何单击下一页 - Selenium in Python: Do not know how to click on next page if link is not present 如何使用 selenium - Python 在 twitter 登录页面中单击下一步 - How to click on next in the twitter sign in page using selenium - Python 为什么这个硒不点击“下一页”直到结束? - Why does this selenium not click “next page” until end? Python Selenium:点击“下一页”按钮 - Python Selenium: Click on "next page" button 抓取网站时如何在Python Selenium中转到下一页直到最后一页? - How to go to next page until the last page in Python Selenium when scraping website? 如何使用硒python动态单击按钮或div标签直到其从页面消失? - How can I click button or div tag dynamically until it disappear from page using selenium python? 在使用Selenium和Python提取数据时,如何通过doPostBack()调用单击“下一步”按钮浏览到下一页? - How to click on the Next button with doPostBack() call to browse to the next page while fetching data with Selenium and Python? python selenium:不等到click()命令后加载页面 - python selenium: does not wait until page is loaded after a click() command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM