0

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);
} 
5
  • Can you show the element locator and the HTML. Commented Jun 25, 2019 at 12:58
  • @NikolayBarakov I have added the screenshot of HTML - I created parent-sibling xpath for "EditSite" button that is xpath="//span[contains(text(), 'eCommerce')]/parent::h4/parent::div/div/div/button[contains(text(), 'Edit Site')]"
    – Saba Malik
    Commented Jun 25, 2019 at 13:33
  • @Moro It is different question. Thanks
    – Saba Malik
    Commented Jun 25, 2019 at 13:34
  • Usually "org.openqa.selenium.NoSuchElementException" occurs when the element is not found on the page or when it's not loaded before the execution of the code. My suggestion is to try slow down the speed of the code execution. If you can share the code inside "GetData.ShowAll.isDisplayed()" method
    – Syrus
    Commented Jun 26, 2019 at 7:03
  • Thank you Joe, I got the solution.
    – Saba Malik
    Commented Jun 26, 2019 at 9:40

1 Answer 1

-1
    `

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

2
  • Thank you Sandeep, I was able to solve it with two ways try/catch and thru List. But the List was more efficient and good approach.
    – Saba Malik
    Commented Jun 26, 2019 at 9:39
  • Hi@SabaMalik it would be great if you can share the code where you have used LIST. Commented Jun 27, 2019 at 7:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.