【问题标题】:How to locate element in selenium for href如何在硒中为href定位元素
【发布时间】:2019-05-25 06:59:33
【问题描述】:

我正在尝试为此定位元素:

<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

【问题讨论】:

  • 首先,检查它是否不在任何 iframe 下。 ( //*[text()=''TIC] ) 试试这个
  • 请分享相关HTML代码
  • 您使用哪种语言绑定? Java/Python/C#??

标签: angularjs selenium xpath css-selectors webdriverwait


【解决方案1】:

只用文字:

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

【讨论】:

    【解决方案2】:

    如果不查看页面的完整代码,很难想出一个准确的定位器,您正在尝试自动化。

    据我目前所见,坚持ABC 文本是有意义的,因此请尝试以下操作:

    1. Partial Link Text

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

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

    【讨论】:

      【解决方案3】:

      尝试使用类 driver.findElement(By.cssSelector("a.cp_text_link"))

      【讨论】:

        【解决方案4】:

        所需元素是 Angular 元素,因此要在元素上定位和调用 click(),您必须诱导 WebDriverWait 以使 元素可点击可以使用以下任一基于 JavaLocator 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();
          

        【讨论】:

          猜你喜欢
          • 2020-02-03
          • 1970-01-01
          • 1970-01-01
          • 2020-08-26
          • 2020-07-23
          • 2019-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多