简体   繁体   English

无法使用Selenium WebDriver和Python选择/单击按钮

[英]Can't select / click button using Selenium WebDriver w Python

I am trying to click a button with Selenium Web Driver. 我试图用Selenium Web Driver单击一个按钮。

(I think it is written in Angular??) (我认为它是用Angular编写的?)

URL is https://www.truelocal.com.au/search/accountants/canberra 网址为https://www.truelocal.com.au/search/accountants/canberra

It's the green button at bottom of page with "Load More Results" 这是页面底部的绿色按钮,带有“加载更多结果”

Element page source is ... 元素页面来源是...

<button class="btn btn-full btn-add js-review-open" ng-class="{true:'btn-loading', false:''}[vm.loadingMore]" ng-hide="vm.checkResultsOffset()" ng-click="vm.loadMoreResults()" aria-hidden="false" style="">

  <!-- ngIf: vm.loadingMore==true -->
  <!-- ngIf: vm.loadingMore==false -->
  <span ng-if="vm.loadingMore==false" class="ng-scope" style="">LOAD MORE RESULTS</span>
  <!-- end ngIf: vm.loadingMore==false -->
</button>

The only thing I can really do is 我唯一能做的就是

elm = driver.find_elements_by_xpath("//*[contains(text(), 'LOAD MORE RESULTS')]")

But I can't get the button to click. 但是我无法单击按钮。

Any help please? 有什么帮助吗?

Try this. 尝试这个。 It will keep clicking on the load more button until there is no such button is left to be clicked. 它将继续单击“加载更多”按钮,直到没有剩下要单击的按钮为止。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.truelocal.com.au/search/accountants/canberra")
wait = WebDriverWait(driver, 10)

while True:
    try:
        link = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[ng-click='vm.loadMoreResults()'] .ng-scope")))
        link.click()
        wait.until(EC.staleness_of(link))
    except:
        break
driver.quit()

To click the button with text LOAD MORE RESULTS we have to wait for the button to render properly as the button is an Angular Element . 要单击带有文本的“ LOAD MORE RESULTS ”按钮,我们必须等待该按钮正确呈现,因为该按钮是Angular Element So you can use the following code block : 因此,您可以使用以下代码块:

WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element(By.XPATH,"//button[@class='btn btn-full btn-add js-review-open']/span[@class='ng-scope']"),'LOAD MORE RESULTS')
driver.find_elements_by_xpath("//button[@class='btn btn-full btn-add js-review-open']/span[@class='ng-scope']").click()

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

相关问题 无法点击这个按钮 selenium webdriver python - Can't click on this button selenium webdriver python 为什么我不能在 selenium webdriver python 上点击这个按钮? - Why i can't click this button on selenium webdriver python? Selenium webdriver 与 python 无法单击网页中的按钮 - Selenium webdriver with python can't click on a button in a webpage Selenium WebDriver 无法点击按钮 - Selenium WebDriver Can't Click Button 无法使用xpath,.select或Selenium使用Python单击“下一步”按钮 - Can't click a “next” button using xpath, .select nor Selenium using Python selenium webdriver 无法使用 driver.find_element_by_css_selector 单击按钮 - selenium webdriver can't click on button using driver.find_element_by_css_selector 使用Python和Selenium WebDriver等待并单击可见按钮 - Using Python and Selenium WebDriver to wait and click on a visible button 尝试使用 Selenium WebDriver (Firefox) 在 Python 中单击按钮 - Attempting to click a button in Python using Selenium WebDriver (Firefox) 使用 Python 和 selenium webdriver 单击没有 ID/名称的按钮元素 - Click on button element without id/name using Python and selenium webdriver 如何使用 Selenium Webdriver 和 Python 单击选项卡按钮 - How to click on the tab button using Selenium Webdriver and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM