简体   繁体   中英

How to go on last page from number of pages using selenium python?

在此处输入图片说明

This is the Screenshot of my UI and HTML DOM.

I want to go to the last page one this I can do is pressing next button many times, but I don't know how many time it should be pressed, so i don't think so its a better way. Can you suggest me better way using selenium commands.

I used this command (pressing next continuously):

driver.find_element_by_xpath('html/body/div[2]/div/section[8]/div/div/div[3]/div/div[2]/table/tfoot/tr/td/div/ul/li[5]/a').click()

You can use try/except like:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

while True:
    try:
         WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//li[not(@class="disabled")]/a[contains(text(), "Next")]'))).click()
    except TimeoutException:
        break

This should allow you to click Next button until last page (until button become disabled)

PS You should not use absolute XPath , eg html/body/div[2] ... /div/ul/li[5]/a as it's sensitive to changes in DOM . Use target element's attributes or its parent/child nodes to create relative XPath

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