I'm trying to figure out a way to see if an element is existing/not existing on a page.
This is what I have so far.
However, if an element is not existing, an exception will be thrown, and the script will stop.
Could anyone help me find a better way to do this?
//Checking navbar links
System.out.println("=======================");
System.out.println("Navbar link checks");
//Checking for Web link element in Nav bar
if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]/a"))!= null){
System.out.println("Web link in Navbar is Present");
}else{
System.out.println("Web link in Navbar is Absent");
}
//Checking for Images link element in Nav bar
if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[2]/a"))!= null){
System.out.println("Images link in Navbar is Present");
}else{
System.out.println("Images link in Navbar is Absent");
}
//Checking for News link element in Nav bar
if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[3]/a"))!= null){
System.out.println("News link in Navbar is Present");
}else{
System.out.println("News link in Navbar is Absent");
}
//Checking for Videos link element in Nav bar
if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[4]/a"))!= null){
System.out.println("Videos link in Navbar is Present");
}else{
System.out.println("News link in Navbar is Absent");
}
//Checking for Maps link element in Nav bar
if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[5]/a"))!= null){
System.out.println("Maps link in Navbar is Present");
}else{
System.out.println("Maps link in Navbar is Absent");
}
You can use a couple of different methods. For this, I'd probably recommend using
findElements
: