[英]Selenium Scraping: Clicking on the Next Button
I am trying to scrape Federal reserve's press releases on https://www.federalreserve.gov/newsevents/pressreleases.htm
, and to scrape documents from previous years, I need to move onto the next page by clicking on Next button at the bottom of the page.我正在尝试在
https://www.federalreserve.gov/newsevents/pressreleases.htm
上抓取美联储的新闻稿,并抓取前几年的文件,我需要通过单击底部的下一步按钮进入下一页页面的。
I have tried a few things, but all of them return a Message: no such element: Unable to locate element:
error, and I can't figure out the issue:我已经尝试了一些东西,但它们都返回了一条
Message: no such element: Unable to locate element:
错误,我无法弄清楚问题所在:
#Attempt 1
driver.find_element_by_css_selector('.pagination-next ng-scope').click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".pagination-next ng-scope"))).click()
#Attempt 2
driver.find_element_by_xpath("//*[@id='article']/ul[2]/li[7]/a").click()
#Attempt 3
element=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH ,"//*[@id='article']/ul[2]/li[7]/a")))
element.click()
Could anyone help me with where I am going wrong?谁能帮我解决我哪里出错了? For ease, the HTML for the Next button is:
为方便起见,下一步按钮的 HTML 是:
<li ng-if="::directionLinks" ng-class="{disabled: noNext()||ngDisabled}" class="pagination-next ng-scope"><a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="" class="ng-binding">Next</a></li>
<a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="" class="ng-binding">Next</a>
Thanks谢谢
The element with text as Next is an <a>
tag and is an Angular element.文本为Next的元素是一个
<a>
标签,是一个Angular元素。
<a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="" class="ng-binding">Next</a>
To click element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :要单击元素,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略:
Using LINK_TEXT :使用LINK_TEXT :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Next"))).click()
Using CSS_SELECTOR :使用CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.pagination-next.ng-scope a.ng-binding"))).click()
Using XPATH :使用XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Next']"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.