简体   繁体   中英

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

To call the function, you can use looping, like while loop

For eg :

while(locatorType, locator):
    element = driver.find_element_by_class_name('next_page')
    element.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM