【问题标题】:Is there any tool available to identify particular text in the href element?是否有任何工具可用于识别 href 元素中的特定文本?
【发布时间】:2021-06-06 14:24:25
【问题描述】:

让我们考虑下面的 href 元素,这里我需要在所有可用的 href 链接中识别 "cn=" 文本。 我的页面有 1000 个 href 元素,因为我需要识别具有 "cn=" 文本的 href url。

href="/software-advice/article/maternity-benifits-of-office-cn=welcome-inc"

【问题讨论】:

    标签: java selenium testing automation automated-tests


    【解决方案1】:

    您可以使用以下css selector 来识别href 值包含cn= 的元素的数量

    List<WebElement> listElement=driver.findElements(By.cssSelector("a[href*='cn=']"));
    System.out.println("Total number of elements :" + listElement.size());
    

    或者识别锚标签,然后获取属性值,然后检查值包含

    List<WebElement> listelement=driver.findElements(By.cssSelector("a[href]"));
            for(WebElement ele:listelement)
            {
               if(ele.getAttribute("href").contains("cn="))
               {
                   System.out.println("Text found");
               }
            }
    

    【讨论】:

      【解决方案2】:

      您可以使用 selenium webDriver。并且下面给出了代码sn-p

                 //Get all the webelement starts with tagname a
                 List<WebElement> urllinks = driver.findElements(By.tagName("a"));
                //Iterate one by one until last element present  
                 for (int i = 0; i < urllinks.size(); i++) {
                  WebElement urllink = urllinks.get(i);
                  //Get Attribute
                  String link = urllink.getAttribute("href");
                  if (link.contains("cn=") {
                   System.out.println("href contains cn=");
                   System.out.println(link)
                  }
      

      【讨论】:

        【解决方案3】:

        当然要使用 XPath

        //a[contains(@href,'cn=')]
        

        它将识别页面上所有包含 cn=href

        如果你确定只有一个元素,那么

        WebElement desiredElement=driver.findElements(By.xpath("//a[contains(@href,'cn=')]"));
        

        【讨论】:

        • 如果此解决方案解决了您的问题,请点赞并将其标记为答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多