How to skip to next command if a button is not found


#1

Hi,

i am trying to write a logic for an alert which is not always shown.
if the alert is shown i will select “No” from the alert. If the alert is not shown i would like to continue to the next button click…

WebElement noteondrosed = driver.findElement(By.id(“com.test.mobiledemo:id/btn_no”));
if (noteondrosed.isDisplayed())
{
noteondrosed.click();
}
else
{
driver.findElement(By.id(“com.test.mobiledemo:id/img_cd_FrontImg”)).click();
}

The above code is failing with the below error

Element info: {Using=id, value=com.test.mobiledemo:id/btn_no}


Check if an element is present
#2

Solution:

if(!driver.findElements(By.id(“com.test.mobiledemo:id/btn_no”)).isEmpty()) {
driver.findElement(By.id(“com.test.mobiledemo:id/btn_no”)).click();
}

	else
	{

	Thread.sleep(40000);
	driver.findElement(By.id("com.test.mobiledemo:id/img_cd_FrontImg")).click();

}