Getting one issue in if else condition, If if-condition gets fasle then else is not working what to do to exicuit else part. I have implemented that logic if one button does not exist then press else one

    if(driver.findElementById("com.mediafoundry.SevenTennis:id/textview_login_menu1").isDisplayed())
        {   
            Thread.sleep(10000);
           driver.findElementById("com.mediafoundry.SevenTennis:id/textview_login_menu1").click();
        }
        else
        {
        driver.findElementById("com.mediafoundry.SevenTennis:id/textview_login_menu").click();
        }

there should not be any other if matching with else u mentioned in u r code, u can try like this

if(driver.findElementById(“com.mediafoundry.SevenTennis:id/textview_login_menu1”).size()>0){
Thread.sleep(10000);
driver.findElementById(“com.mediafoundry.SevenTennis:id/textview_login_menu1”).click();
}
else if (driver.findElementById(“com.mediafoundry.SevenTennis:id/textview_login_menu”).size()>0)
driver.findElementById(“com.mediafoundry.SevenTennis:id/textview_login_menu”).click();

I think you should do it with try/catch , because is if element does not exists , then driver.findElementById will enter infinite loop or will drop out with element not found exception .

try {
// try to find element
} catch ( Exception e) {
// find another element
}

I would search the elements and create a list. The if/else statement you created will ensure the test fails if the element doesn’t exist. .

  // Search for Login Menu 1 
  List<WebElement> loginMenu = driver.findElementsById("com.mediafoundry.SevenTennis:id/textview_login_menu1");

    if (!loginMenu.isEmpty()) {
        // Click Login Menu 1
        loginMenu.get(0).click();
    } else {
        // Click Login Menu
        driver.findElementById("com.mediafoundry.SevenTennis:id/textview_login_menu").click();
    }

almost nice but little correction to fit author initial needs (element could exists but can be not visible)

List<WebElement> loginMenu = driver.findElementsById("com.mediafoundry.SevenTennis:id/textview_login_menu1");
if (!loginMenu.isEmpty && loginMenu.isDisplayed())
   loginMenu.click();
else
   driver.findElementById("com.mediafoundry.SevenTennis:id/textview_login_menu").click();
1 Like

its working with try catch. thanks all