【问题标题】:Selenium (Java) unable to find element by visible link textSelenium(Java)无法通过可见链接文本找到元素
【发布时间】:2019-12-16 14:24:45
【问题描述】:

我遇到了一个更令人沮丧的 Selenium 问题。

我在页面上有一个表格,其中一个元素有一个链接。该链接称为“Send brev”。

<td data-e2e-selector="sakHendelser" id="sakHendelser_0" class="ng-star-inserted">
  <saksnytt><!----><!----><!---->
   <div class="hb-tekst--ingenBryting ng-star-inserted">
    <!----><!----><!---->
     <button class="hb-knapp hb-knapp--lenke hb-knapp--alignTeks hb-knapp-lenke-nopadding ng-star-inserted" 
      data-e2e-selector="saksnytt-link">
        <i aria-hidden="true" class="fa fa-flag fa-envelope"></i>
        <span class="hb-knapp-tekst">Send brev </span></button> &nbsp;&nbsp;
        <!----><span aria-hidden="true" class="fa fa-info-circle ng-star-inserted" 
        id="reservert-info">
        </span><!----><!----><!----></div><!---->
</saksnytt></td>

这曾经有效,我还没有找到它现在停止工作的原因。但无论我如何尝试找到它,Selenium 都会以

 no such element: Unable to locate element: {"method":"xpath","selector":"//data-e2e-selector[contains(text(),'Send brev ')]"}

或类似的-

我尝试了以下方法:

Browser.Wait().until(presenceOfElementLocated(By.linkText(merknad + " ")));

这超时了。

driver.findElement(By.linkText(merknad + " ")).click();
driver.findElement(By.partialLinkText(merknad + " ")).click();
driver.findElement(By.xpath("//data-e2e-selector[contains(text(),'" + "Send brev" +"')]"));    
driver.findElement(By.xpath("//id[contains(text(),'" + "Send brev" +"')]"));
data-e2e-selector="sakHendelser" id="sakHendelser

我也尝试在链接文本中的“v”之后添加一个空格,但没有成功。

Selenium 怎么找不到像这样清晰可见且可交互的元素?

由于代码是在 Angular 中生成的,并且或多或少是动态的,所以我也无法为这个特定元素创建标签,所以我只能尝试通过文本来查找它。我无法上班。

有什么想法吗?

我可以用这个代码点击它:

driver.findElement(By.xpath("//*[@id='sakHendelser_0']/saksnytt/div/button/span")).click();

但我希望能够将链接文本发送到方法,而不是像那样对 xpath 进行硬编码。

【问题讨论】:

  • 文本是Send brev,为什么要像linkText(merknad + " ")By.partialLinkText(merknad + " ")那样添加merknad

标签: java selenium


【解决方案1】:

LinkText 和 PartialLinkText 仅适用于 A 标签内的文本。你没有给你的HTML,所以这不起作用。在给定包含文本的情况下定位元素的唯一选择是使用 XPath。

【讨论】:

    【解决方案2】:

    通过将// 放在属性(例如//id//data-e2e-selector)之前使用XPath 的方式不正确。这些是元素属性,而不是标签。

    文本Send brev 包含在span 元素中,因此尝试通过查询data-e2e-selector 来定位它是行不通的——这也不是WebElement,它是button 上的一个属性。

    因为你提到这个项目有一些你想用作参数的可见链接文本,所以我会只查询文本并使用contains 来解决任何隐藏的空白:

    driver.findElement(By.xpath("//span[contains(text(), 'Send brev')]")).click();
    

    您可以像这样使用链接文本作为参数:

    string linkText = "Send brev"
    driver.findElement(By.xpath("//span[contains(text(), '" + linkText + "')]")).click();
    

    您可能会更幸运地点击button 本身:

    driver.findElement(By.xpath("//button[span[contains(text(), '" + linkText + "')]]")).click();
    

    【讨论】:

      【解决方案3】:

      尝试在 xpath 中使用 normalize-space:

      //span[normalize-space(text())='Send brev']

      【讨论】:

        【解决方案4】:

        您需要注意以下几点:

        • By.linkText()By.partialLinkText() 仅适用于 &lt;a&gt; 标签,因为所需元素位于 &lt;span&gt; 标签内。
        • data-e2e-selectorid 是 WebElement 的属性,但您尝试将它们用作 tagName
        • Send brev 文本包含在 &lt;span&gt; 标记中。
        • 因此,将带有文本的元素标识为 Send brev 并继续与它进行交互,您需要为 elementToBeClickable() 诱导 WebDriverWait,您可以使用以下任一Locator Strategies

        • cssSelector:

          new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.hb-knapp.hb-knapp--lenke.hb-knapp--alignTeks.hb-knapp-lenke-nopadding.ng-star-inserted[data-e2e-selector='saksnytt-link'] span.hb-knapp-tekst"))).click();
          
        • xpath:

          new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='hb-knapp hb-knapp--lenke hb-knapp--alignTeks hb-knapp-lenke-nopadding ng-star-inserted' and @data-e2e-selector='saksnytt-link']//span[@class='hb-knapp-tekst' and contains(., 'Send brev')]"))).click();
          

        【讨论】:

          猜你喜欢
          • 2013-12-21
          • 1970-01-01
          • 2019-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-07
          相关资源
          最近更新 更多