【问题标题】:How to check particular tag exist or not using webdriver with javajava - 如何使用带有java的webdriver检查特定标签是否存在
【发布时间】:2018-06-11 23:44:27
【问题描述】:

从下面的 html 中,我想检查 <article> 标签是否可用或不使用带有 java 的 webdriver

HTML 代码

<div id="patentResultList">
<article>
<div class="search_section_title"></div>
<div class="search_basic_info"></div>
</article>
</div>

首先我需要检查if article tag 是否可用,如果可用,然后我移动到 div 标记内部,如果没有将跳过。 请指导我。

【问题讨论】:

标签: java html xpath selenium-webdriver


【解决方案1】:

要检查&lt;article&gt; 标签是否可用,您可以使用以下代码块:

if(driver.findElements(By.tagName("article")).size()>0)
    System.out.println("article tag is present");
else
    System.out.println("article tag is not present");

【讨论】:

    【解决方案2】:

    感谢您的帮助, 以下是其工作正常的答案

     public Boolean checkResultAvailableOrNot() {
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            boolean checkresultGrid;
            try {
                WebElement tf = driver.findElement(By.xpath("//*[@id='patentResultList']/article"));
                tf.isDisplayed();
                checkresultGrid = true;
            } catch (NoSuchElementException e) {
                checkresultGrid = false;
            } finally {
                driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
            }
            return checkresultGrid;
        }
    

    【讨论】:

      【解决方案3】:

      如果在第一行代码中找到了元素,则执行下一行,您可以编写代码来查找相应的元素,否则在找不到元素时捕获异常。

      try{
           driver.findElement(By.tagName("article"));
           // write code to do any operation on the element
      }
      catch (NoSuchElementException e) {
            System.out.println("Article element is not present");
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-24
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 2017-03-28
        • 2012-05-06
        • 2014-01-28
        • 2014-07-19
        • 1970-01-01
        相关资源
        最近更新 更多