【问题标题】:How to move to next next page on Python Selenium throught the rel_name?如何通过实名跳转到 Python Selenium 的下一页?
【发布时间】:2021-09-16 13:31:01
【问题描述】:

我正在尝试为房屋租赁网站构建爬虫,但我无法转到下一页。并显示以下错误:

line 44, in <module> browser.find_element_by_xpath("//div[@class='pagination_hi']/[rel='next']").click()

这是页码所在的元素:

<div class="pagination_hi">
previous_page
</div><div class="pagination_hi">
<a href="https://www.28hse.com/rent/page-2" rel="next">next_page</a>

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python selenium xpath


【解决方案1】:

在 Selenium 中有 4 种点击方式。

我将使用这个 xpath

//a[text()='next_page']

代码试用 1:

time.sleep(5)
browser.find_element_by_xpath("//a[text()='next_page']").click()

代码试用 2:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='next_page']"))).click()

代码试用 3:

time.sleep(5)
button = browser.find_element_by_xpath("//a[text()='next_page']")
browser.execute_script("arguments[0].click();", button)

代码试用 4:

time.sleep(5)
button = browser.find_element_by_xpath("//a[text()='next_page']")
ActionChains(browser).move_to_element(button).click().perform()

进口:

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.common.action_chains import ActionChains

【讨论】:

  • 当我使用你提供的代码试用 1 时,我会得到“AttributeError: module 'webdriver_manager.driver' has no attribute 'find_element_by_xpath”
  • 这是因为我使用driver 代替browser,请参阅上面的更新代码
【解决方案2】:

正确的 XPath 语法应该是

//div[@class='pagination_hi']/a[@rel='next']

您也可以尝试通过链接文本定位节点:

browser.find_element_by_link_text("next_page").click()

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多