How to find an element that is not existing on the page with selenium webdriver testng framework

Hi.
Selenium webdriver is not coming out of the page if asking it to find an element that is actually not present.
i have to pass the test if a particular button is not present on the page.
it is not stopping the execution, i have to close the browser manually then only able to see the results in console.

sri Default Asked on May 26, 2015 in Selenium WebDriver.

Please elaborate your question or post the sample code that you are using.

on May 27, 2015.

thanks.
Im new to selenium.  i have this scenario where i click a name link on page from a group of names, and it takes to next page called  details page.  For some Names  the page will shows a button called “change”.  I m able to find this button . no problem with that. But for certain devices the page should not show or not display this “change” button.
i need to check whether this button is present on this page for that particular Name and pass the test if its not existing.
Further , im able to see the  result as passed  in eclipse console only when i manually close the browser.  Its not quitting or closing or stopping the execution even when i gave the driver.quit().   Its working perfectly when the button is exiting but  not when asking the selenium to find the button that is not existing.

i used the following options in my code separately

1.
WebElement element = driver.findElement(By.xpath(“//*[@id=’Name’]”));
Assert.assertFalse(element.isDisplayed());
System.out.println(” Button is not present”);
Reporter.log(” button not present”);

2.
WebElement Element = driver.findElement(By.xpath(“//*[@id=’Name’]”));
if(Element == null){
Assert.assertFalse(true);
//driver.quit();
}
driver.quit();

3.
if (driver.findElement(By.xpath(“//*[@id=’Name’]”))!=null)
{
Reporter.log(“Change  button is DISPLAYED on  details page for a Non User, hence test failed”);
System.out.println(“change  button IS present on  details page”);
driver.quit();
}
else
{
Reporter.log(“Change  button is NOT DISPLAYED on  details page for a Non User, hence test passed “);
System.out.println(“change owner button NOT present on device details page”);
}
driver.quit();

4.
List<WebElement> E = driver.findElements(By.id(“Name”));
int NE = E.size();
if(NE == 0)
System.out.println(“Test passed”);
driver.quit();

on May 27, 2015.
Add Comment
1 Answer(s)

You’ve mentioned ‘But for certain devices the page should not show or not display this “change” button.’  That button can be specific to those devices. Hence you cannot find or locate that in browser obviously.

This answer is given depends on my understanding on your question. :)

vijayranha Default Answered on May 27, 2015.

thanks vijay.
But still i need to check whether that button is displayed for that device , because it will be not displayed after some actions on that device have been performed. so i perform some actions on that device and after that i  have to  come back to this details page and verify  if the button is still present or not, if not present then the test has to be passed.

on May 27, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.