【问题标题】:Can't select / click button using Selenium WebDriver w Python无法使用 Selenium WebDriver w Python 选择/单击按钮
【发布时间】:2017-11-27 20:25:58
【问题描述】:

我正在尝试使用 Selenium Web 驱动程序单击一个按钮。

(我认为它是用 Angular 编写的??)

网址是https://www.truelocal.com.au/search/accountants/canberra

这是页面底部的绿色按钮,带有“加载更多结果”

元素页面源是...

<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>

我唯一能做的就是

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

但我无法点击按钮。

有什么帮助吗?

【问题讨论】:

  • 您是否得到任何相关的堆栈跟踪来显示测试失败的原因?

标签: python selenium-webdriver web-scraping


【解决方案1】:

试试这个。它会一直点击加载更多按钮,直到没有可以点击的按钮。

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()

【讨论】:

  • 谢谢。感谢您的帮助@Shahin
【解决方案2】:

要单击带有文本LOAD MORE RESULTS 的按钮,我们必须等待按钮正确呈现,因为该按钮是Angular Element。所以你可以使用下面的代码块:

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()

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2017-08-21
    • 2021-03-31
    • 2016-03-02
    • 2020-03-16
    相关资源
    最近更新 更多