繁体   English   中英

Python selenium 使用 find_element_by 点击按钮

[英]Python selenium click on button using find_element_by

我正在尝试从https://www.federalreserve.gov/newsevents/speeches.htm中抓取一些演讲并自动完成,我需要单击“下一步”按钮从所有页面中抓取。

不幸的是,无论我使用什么类型的find_element_byby_xpathby_css_selector ,...),我都会不断收到NoSuchElementException 我也尝试过driver.execute_script()但它不起作用。

这是我为 XPath 和 CSS 选择器尝试的内容:

driver.find_element_by_css_selector("li.pagination-next:nth-child(11) > a:nth-child(1)")
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[4]/ul[1]/li[11]/a')

这是按钮的 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>

您可以使用以下代码单击该元素:
我已经使用 xpath 中的按钮文本来找到它,并使用了显式等待,以便脚本等待元素出现在页面上。

next_button = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[text()='Next']")))
next_button.click()

您需要添加以下导入:

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.

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