1

I want to put an if condition, when id="prog_bg" or class="progress_box_bg" is present then execute the remaining code. DOM is as below,

<div id="wizard">
 <div class="quote_header">
 <div class="items">
      <div id="top-line" style="display: block;">
            <div class="back_box">
             <div class="progress_box">
                 <div id="prog_bg" class="progress_box_bg" style="width: 75px;"></div>
             </div>
             </div>
        <div id="service-div" class="page" style="padding-top:45px; >">
        <div id="propertytype-div" class="page">

I tried with lots of option but it won't work . Guys let me know how do it?

  1. if (var.equals(driver.findElement(By.tagName("body")).getText().contains("prog_bg")))
  2. try { if (selenium.getHtmlSource().matches("^[\\s\\S]*prog_bg[\\s\\S]*$")) break; } catch (Exception e) {};
  3. if(driver.getPageSource().matches("^[\\s\\S]*prog_bg[\\s\\S]*$"))
  4. if(driver.findElement(By.id("prog_bg")).isDisplayed())
  5. if (driver.findElement(By.className("progress_box_bg")).isDisplayed())

Thanks, Sachin

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
  • Possible duplicate of [best way to check that element is not present webDriver selenium with java](http://stackoverflow.com/questions/12270092/best-way-to-check-that-element-is-not-present-webdriver-selenium-with-java) – Alex Okrushko Jan 23 '13 at 15:25

3 Answers3

0

Could you try something like this? You can replace Assert with if()

boolean ispresent= driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*prog_bg:[\\s\\S]*$");//TODO: Make this dynamic
Assert.assertEquals(true, ispresent);

boolean ispresent= driver.findElement(By.id("prog_id"))
Assert.assertEquals(true, ispresent);
Oleaha
  • 130
  • 1
  • 2
  • 10
0

You could also try something like this.

// Find element by id
boolean isElementPresent = driver.findElement(By.id("prog_id"));
// Find element by class
boolean isElementPresent = driver.findElement(By.className("progress_box_bg"));
//This  writes out an errormessage if isElementPresent equals false, good for reports!
Assert.assertTrue("Could not find the element", isElementPresent);
Oleaha
  • 130
  • 1
  • 2
  • 10
0

Numbers 4 and 5 are on the right track. However, you should not be calling isDisplayed() if you just want to check its presence in the DOM. isDisplayed() checks the element presence in the DOM and then checks to see if the element is visible to the user.

Instead, you should just try to find the element itself:

if(driver.findElement(By.id("prog_bg")) || driver.findElement(By.className("progress_box_bg")) {
/*Execute code here*/
}

Also, please note that element attributes do not appear in the text of the page's body, the only appear in the DOM. Any attempts to find these elements like you do in numbers 1, 2, and 3 are completely futile.

bbbco
  • 1,508
  • 1
  • 10
  • 25