Python Selenium继续点击下一步按钮

时间:2018-02-17 05:25:02

标签: python selenium

我正在尝试使用Python Selenium抓取多个页面 https://www.walmart.com/ip/Clorox-Disinfecting-Wipes-On-The-Go-Citrus-Blend-Scent-34-Wipes/29701960?page=seeAllReviews

网页底部有按钮。 HTML如下所示:

<ul class="paginator-list">
<li><button aria-label="Page 1 of 6 selected" class="active">1</button></li>
<li><button aria-label="Page 2 of 6 " class="">2</button></li>
<li><button aria-label="Page 3 of 6 " class="">3</button></li>
<li><button aria-label="Page 4 of 6 " class="">4</button></li>
<li><button aria-label="Page 5 of 6 " class="">5</button></li>
<li class="paginator-list-gap"></li>
<li><button aria-label="Page 3141 of 6 " class="">3141</button></li>
</ul>

如何使用Selenium点击第二个按钮(第2页,共6页)?当页面更改时,如何继续单击下一步按钮。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您可以使用此xpath每次单击nxt按钮链接 ://button[@class="active"]/ancestor::li/following-sibling::li[1]

答案 1 :(得分:1)

根据您分享的 HTML 和您点击第二个按钮(第2页,共6页)的评论,您必须滚动分页 webelement Viewport内,然后按如下方式调用click()

from selenium import webdriver

driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.walmart.com/ip/Clorox-Disinfecting-Wipes-On-The-Go-Citrus-Blend-Scent-34-Wipes/29701960?page=seeAllReviews")
pagination_element = driver.find_element_by_xpath("//div[@class='ReviewsFooter-pagination arrange arrange-spaced']")
driver.execute_script("return arguments[0].scrollIntoView(true);", pagination_element)
driver.find_element_by_xpath("//div[@class='ReviewsFooter-pagination arrange arrange-spaced']//ul[@class='paginator-list']/li/button[@aria-label='Page 2 of 6 ']").click()
print("Clicked on Page 2")
driver.quit()

控制台输出:

Clicked on Page 2