简体   繁体   English

如何使用 selenium python 从页数转到最后一页?

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

在此处输入图片说明

This is the Screenshot of my UI and HTML DOM.这是我的 UI 和 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.你能建议我使用 selenium 命令更好的方法吗?

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:您可以使用try/except例如:

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)这应该允许您单击“ Next按钮直到最后一页(直到按钮被禁用)

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 . PS 你不应该使用绝对XPath ,例如html/body/div[2] ... /div/ul/li[5]/a因为它对DOM变化很敏感。 Use target element's attributes or its parent/child nodes to create relative XPath使用目标元素的属性或其父/子节点来创建相对XPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抓取网站时如何在Python Selenium中转到下一页直到最后一页? - How to go to next page until the last page in Python Selenium when scraping website? 如何使用 selenium python 和不同等级的页面从带有滚动的 web 页面中提取所有数据? - how to extract all data from a web page with a scroll using selenium python and different rank pages? 使用 Python Selenium 从网页中提取页码 - Extract Page number from a web page using Python Selenium 使用 Selenium python 从下拉列表中选择页码 - Selecting page number from dropdown using Selenium python 使用python从html获取最后一个页码 - Get last page number from html using python 如何使用 Selenium Python 从 class 中选择最后一个元素 - How to choose last element from class using Selenium Python 如何使用 Selenium Python 迭代谷歌页面 - How to iterating google pages using Selenium Python Python (Selenium) - 如何将数据从第 1 页到最后一页保存到同一个 CSV - Python (Selenium) - How to save data from page 1 to the last page to the same CSV 在 Selenium 中用 Python 抓取多个页面时遇到问题 - 仅拉取第一页或最后一页 - Having trouble scraping multiple pages in Python in Selenium- only pulls first page, or last page 用Selenium和美丽的汤进行Python Web爬行所有页面(转到下一页) - Python Web Crawling with Selenium & Beautiful soup All pages(go to next page)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM