使用硒单击下一页

编程入门 行业动态 更新时间:2024-10-27 14:26:25
本文介绍了使用硒单击下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have tried a number of ways, from xpath to link text to get Selenium to locate the "Next Page" button, and then click until the last page, but just couldn't get this to work.

这是我使用xpath的错误:

This is the error that I have using xpath:

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

no such element: Unable to locate element: {"method":"xpath","selector":"//li[@class="next"]/a"} (Session info: chrome=79.0.3945.88)

代码:

import requests, bs4, time, selenium #import libraries from selenium import webdriver from selenium.webdriver.support.select import Select driver=webdriver.Chrome() driver.get('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') url=driver.current_url print(url) driver.get(url) nxt=driver.find_element_by_xpath('//li[@class="next"]/a') nxt.click() time.sleep(1)

推荐答案

要获取硒找到下一页»,然后单击直到需要引导 WebDriverWait 的最后一页,您可以使用以下定位器策略:

To get Selenium to locate the Next Page » and then click until the last page you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • 代码块:

  • Code Block:

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdrivermon.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import Select from seleniummon.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('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()

  • 控制台输出:

  • Console Output:

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

  • 更多推荐

    使用硒单击下一页

    本文发布于:2023-06-13 05:21:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/674850.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:下一页   单击

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!