简体   繁体   English

Python Selenium继续单击下一步按钮

[英]Python Selenium keep clicking next button

I am trying to crawl multiple pages using Python Selenium from https://www.walmart.com/ip/Clorox-Disinfecting-Wipes-On-The-Go-Citrus-Blend-Scent-34-Wipes/29701960?page=seeAllReviews . 我正在尝试从https://www.walmart.com/ip/Clorox-Disinfecting-Wipes-On-The-Go-Citrus-Blend-Scent-34-Wipes/29701960?使用Python Selenium爬网多个页面

There are buttons at the bottom of the web page. 网页底部有按钮。 HTML looks like below: 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>

How do I click on second button (Page 2 of 6) using Selenium? 如何使用硒单击第二个按钮(第2页,共6页)? How do I keep clicking on next button as page changes. 当页面更改时,如何继续单击下一步按钮。 Any suggestions? 有什么建议么?

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

As per the HTML you have shared and your comment to click on 2nd button (Page 2 of 6) you have to scroll the pagination webelement within the Viewport and then invoke click() as follows : 按照您共享的HTML和单击第二个按钮(第2页,共6页)的注释,您必须在Viewport中滚动分页webelement ,然后click()如下所示调用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()

Console Output : 控制台输出:

Clicked on Page 2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM