if statement throws an exception "org.openqa.selenium.NoSuchElementException"



  • enter image description here There is a requirement to check the link "Show All" on page. If It is displayed on page click on that link to expand all the sites on page, then search and click on "Edit Site" button that is starting with eCommerce text and If the link "Show All" is not displayed on the page, just look for "Edit Site" button that is starting with eCommerce text and click on it.

    I wrote below code for it but It is not working as expected. The if else clause causes org.openqa.selenium.NoSuchElementException exception. I am sure that I am making some mistake in if else statement. I am new on selenium, Please help me. Thank you.

    if (GetData.ShowAll.isDisplayed()) {
    
            System.out.println("Show All button is displayed");
    
            Click("Please click Show All button",GetData.ShowAll);
    
            Click("Please click Edit Site button for eCommerce Platinium",GetData.EditSite1);
    
    
        }
    
    else if  (!GetData.ShowAll.isDisplayed())
    
        System.out.println("Show All button is not displayed");
    
        Click("Please click Edit Site button for eCommerce Platinium",GetData.EditSite1);
    } 
    


  •     `
    
    boolean isElementPresent;
    
    try {
    isElementPresent = driver.findElement("Locator of your element").isDisplayed();
    }
    catch (Exception e){
    isElementPresent = false;
    }
    if(isElementPresent){
    //do the operation you want to
    }
    else{
    system.out.println("Element is not present");
    }
    

    Try this code



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2