【问题标题】:How to click on Angular element via selenium webdriver?如何通过 selenium webdriver 点击 Angular 元素?
【发布时间】:2020-04-09 18:30:48
【问题描述】:

在下面需要更多帮助才能单击()稍后询问我按钮。

<div align="center" class="mar-b0">
  <button class="custom-green md-button md-ink-ripple no-margin md-button"
          type="button" ng-transclude="" ng-click="vm.goHome();">
     <span class="ng-binding ng-scope">Ask me later</span>
  </button>
</div> 

我试过下面的代码 -

new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(
     By.xpath("//span[@class='ng-binding ng-scope' and contains(.,
              'ASK ME LATER')]"))).click();

【问题讨论】:

  • 下面是代码-
  • 当你的代码执行时会发生什么是点击还是抛出任何异常?

标签: angularjs xpath css-selectors webdriver webdriverwait


【解决方案1】:

该元素是Angular 元素,因此对于click() 元素,您必须为element to be clickable 诱导WebDriverWait,您可以使用以下任一Locator Strategies

  • 使用cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.custom-green.md-button.md-ink-ripple.no-margin.md-button[ng-click*='goHome']>span.ng-binding.ng-scope"))).click();
    
  • 使用xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='custom-green md-button md-ink-ripple no-margin md-button' and contains(@ng-click, 'goHome')]/span[@class='ng-binding ng-scope' and text()='Ask me later']"))).click();
    

【讨论】:

  • 感谢您的支持。上面的代码有效,但只能工作一次。我需要初始化它还是请建议如何进行。
  • @ShipraSaxena 此解决方案特定于您为其提供了 HTML 的 &lt;button&gt; 元素。
猜你喜欢
相关资源
最近更新 更多
热门标签