【问题标题】:How to find a element with specific text in <span> with Selenium如何使用 Selenium 在 <span> 中查找具有特定文本的元素
【发布时间】:2018-11-11 11:00:23
【问题描述】:

我正在使用 Selenium WebDriver。我面临动态更改 ID 的问题。我浏览过一些帖子,例如Handle elements that have changing ids all the time through Selenium Webdriver,但没有找到可以帮助我的解决方案。

在 XPath 中包含 start-withcontains 无济于事,因为有很多以相似文本开头的元素和 id 不断变化。请参阅下面的HTML 以获得更清晰的信息。

<mat-option _ngcontent-c1="" class="__mat-option mat-option ng-star-inserted" 
     role="option" tabindex="0" id="mat-option-4" 
     aria-selected="false" aria-disabled="false">
   <span class="mat-option-text">
        <i _ngcontent-c1="" class="material-icons">description</i>
        <span _ngcontent-c1="" style="padding-left: 8px">Campaign Details</span>
   </span>
  <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
</mat-option>
  • 请注意上面 HTML 中的 ID 是动态的并且不断变化。
  • 像这样具有相同类名的元素很少。
  • 还有几个以mat-option- 开头的元素。
  • 唯一独特的是 &lt;span&gt; 中的文字。

我尝试了driver.findElement(By.xpath(".//span[contains(text(), 'Campaign Details']"));,但不适合我。

【问题讨论】:

  • 您遇到的错误是什么?
  • 请澄清你想要这个元素&lt;span _ngcontent-c1="" style="padding-left: 8px"&gt;Campaign Details&lt;/span&gt;或其祖先:&lt;mat-option&gt;
  • 是的,想找Campaign Details元素。

标签: java selenium selenium-webdriver xpath css-selectors


【解决方案1】:

根据您提供的 HTML,将带有文本的元素标识为 Campaign Details,因为该元素是 Angular 元素,您必须诱导 WebDriverWait em> 让 元素可见 如下:

WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//mat-option[@class='__mat-option mat-option ng-star-inserted' and @role='option'][starts-with(@id,'mat-option-')]/span[@class='mat-option-text']//span[contains(.,'Campaign Details')]")));

【讨论】:

  • 注意这一点:By.id("//mat-option[@class='__mat-option mat-option ng-star-inserted' and @role='option'][starts -with(@id,'mat-option-')]/span[@class='mat-option-text']//span[contains(.,'Campaign Details')]" ,id不能有/ /span 和你写的所有东西。把它改成 xpath。
  • 与上面的问题类似,有没有办法找到 HTML &lt;h4 _ngcontent-c8="" class="text-green" maxcounter="" style="margin-top: 3px;"&gt; RM 10,000 &amp;nbsp; &lt;br _ngcontent-c8="" class="ng-tns-c8-13"&gt; &lt;sup _ngcontent-c8="" class="ng-tns-c8-13" style="color: darkgray; font-weight: bold;"&gt;* excluding GST&lt;/sup&gt; &lt;/h4&gt; 以下的元素。我想找到 RM 10,000。这个数额是动态的。
  • @cruisepandey 对我有什么建议吗?
  • @Sanchit :请提出一个单独的问题。
  • 会不会是重复的。肯定有人会投反对票。
【解决方案2】:

唯一独特的是&lt;span&gt;中的文字所以文字是:Campaign Details

WebElement campaignDeatils =  driver.findElement(By.xpath("//i[text()='description']/following-sibling::span[text()='Campaign Details']"))  
campaignDeatils.getText(); // or any other operation you wanna do.

【讨论】:

  • 谢谢@cruisepandey 我已经尝试过了,但对我不起作用。 :-(
  • @ApoorvaSoni:试试这个:driver.findElement(By.xpath("//i[text()='description']/following-sibling::span[text()='Campaign详情']"));
【解决方案3】:
WebElement campaignDeatils =  driver.findElement(By.xpath(""//i[@class='fa fa-bullhorn']/following-sibling::span[text()='Campaigns']"))  

campaignDeatils.getText();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-10
    • 2020-01-13
    • 2013-02-13
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多