I'm writing an automation test for a web page, where User has to click on a button based on its presence
I'm using Selenium WebDriver with Java
Scenario
1. Only one button can be present at a time (button1
or button2
)
2. If button1
is present, User clicks on that
3. If button2
is present, User clicks on that
Code:
if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div")).isDisplayed() ) {
//clicking on button1 if its presemt
WebElement clickBtn1 = driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[2]/div/a/div"));
clickBtn1.click();
}
else if(driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div")).isDisplayed() ){
//Clicking on button2 if its present
WebElement clickBtn2 = driver.findElement(By.xpath("//*[@id='product_container_1']/div[2]/div[2]/div[3]/div[3]/div[2]/div"));
clickBtn2.click();
}
else
{
System.out.println("No such button found");
}
The above code snippet doesn't work. Can someone, please help to correct it.
Note: If I remove the if-else conditions and run separately for each button functions it works
Thanks,