使用硒单击下一页

时间:2019-12-28 02:26:19

标签: python selenium selenium-webdriver

我尝试了多种方法,从xpath到链接文本,以获取Selenium来定位“下一页”按钮,然后单击直到最后一页,但无法使其正常工作。

这是我使用xpath的错误:

没有这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // li [@ class =” next“] / a”}   (会话信息:chrome = 79.0.3945.88)

代码:

from __pkginfo__ import __version__, __author__

2 个答案:

答案 0 :(得分:0)

如下图所示,页面中有两个元素与您定义的xpath语法相同。

enter image description here

为了获得第二个值,您需要从父xpathdiv定义nav

import requests, bs4, time, selenium  #import libraries
from selenium import webdriver
from selenium.webdriver.support.select import Select

driver=webdriver.Chrome()
driver.get('https://egov2.manchesternh.gov/Click2GovTX/accountsearch.html')

select=Select(driver.find_element_by_id('searchMethod'))
select.select_by_value('2')

streetName=driver.find_element_by_id('addressName')
time.sleep(1)
streetName.clear()
streetName.send_keys("A")
streetName.send_keys(u'\ue007')

nxt=driver.find_element_by_xpath('//div[@class="panel-body"]/nav[2]//li[@class="next"]/a')
nxt.click() 
time.sleep(1)

答案 1 :(得分:0)

要获取Selenium来找到下一页»,然后单击直到最后一页,您需要为element_to_be_clickable()引入 WebDriverWait ,并您可以使用以下Locator Strategies

  • 代码块:

        from selenium import webdriver
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        from selenium.webdriver.support.ui import Select
        from selenium.common.exceptions import TimeoutException
    
        options = webdriver.ChromeOptions() 
        options.add_argument("start-maximized")
        options.add_experimental_option("excludeSwitches", ["enable-automation"])
        options.add_experimental_option('useAutomationExtension', False)
        driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
        driver.get('https://egov2.manchesternh.gov/Click2GovTX/accountsearch.html')
        select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#searchMethod"))))
        select.select_by_value('2')
        streetName = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[name='addressName']")))
        streetName.send_keys("A")
        streetName.send_keys(u'\ue007')
        while True:
            try:
                WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p//following::nav[2]//ul//li[@class='next']//a"))).click()
                print("Clicked on  Next Page »")
            except TimeoutException:
                print("No more Next Page »")
                break
        driver.quit()
    
  • 控制台输出:

        Clicked on  Next Page »
        Clicked on  Next Page »
        Clicked on  Next Page »
        Clicked on  Next Page »
        Clicked on  Next Page »
        .
        .
        .
        No more Next Page »