1

From the below html i want to check if the <article> tag available or not using webdriver with java

HTML code

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

First i need to check if article tag available or not if available then i move to inside the div tag, if not will skip. please guid me.

Prabu
  • 3,550
  • 9
  • 44
  • 85
  • Possible duplicate of [How to verify element present or visible in selenium 2 (Selenium WebDriver)](https://stackoverflow.com/questions/14156656/how-to-verify-element-present-or-visible-in-selenium-2-selenium-webdriver) – Cardinal System Jan 02 '18 at 08:59

3 Answers3

1

To check if the <article> tag is available or not you can use the following code block :

if(driver.findElements(By.tagName("article")).size()>0)
    System.out.println("article tag is present");
else
    System.out.println("article tag is not present");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Thanks for the help, below is the answer its working fine

 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;
    }
Prabu
  • 3,550
  • 9
  • 44
  • 85
0

If the element is found in the first line of code then the next line gets executed where you can write code to find the corresponding elements, else the exception is caught when the element is not found.

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");
}
Melvin Richard
  • 403
  • 1
  • 7
  • 15