如何在href中找到硒元素

时间:2019-05-25 06:59:33

标签: angularjs selenium xpath css-selectors webdriverwait

我正在尝试为此查找元素

<a ng-if="showLink &amp;&amp; customer.partnerType == 2 &amp;&amp; customer.isDirectCustomer" class="cp_text_link ng-binding ng-scope" ng-href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&amp;orgName=Attunix" href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&amp;orgName=ABC">
  ABC
</a>

我尝试了XPath和CssSelector,但是找不到元素。 有人可以帮我找到这个元素吗 TIA

4 个答案:

答案 0 :(得分:2)

仅使用文本:

in self

答案 1 :(得分:1)

要想找到一个准确的定位器而又不看页面的完整代码是相当困难的,而您正在尝试实现自动化。

从目前为止我所看到的,坚持使用ABC文本是有意义的,因此请尝试以下操作:

  1. Partial Link Text

    driver.findElement(By.partialLinkText("ABC"));
    
  2. 或等效的XPath

    driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));
    

答案 2 :(得分:0)

尝试使用课程 driver.findElement(By.cssSelector(“ a.cp_text_link”))

答案 3 :(得分:0)

所需元素是Angular元素,因此要在该元素上定位并调用click(),必须诱使 WebDriverWait 使元素可点击,您可以使用以下基于 Java Locator Strategies之一:

  • partialLinkText

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("ABC"))).click();
    
  • cssSelector

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cp_text_link.ng-binding.ng-scope[ng-href*='orgId'][href$='ABC']"))).click();
    
  • xpath

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='cp_text_link ng-binding ng-scope' and contains(@ng-href, 'orgId')][contains(@href, 'ABC')]"))).click();
    
相关问题