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);
} 

marked as duplicate by Moro, João Farias, Bharat Mane, IAmMilinPatel, alecxe Jul 1 at 1:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Can you show the element locator and the HTML. – Nikolay Barakov Jun 25 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 Jun 25 at 13:33
  • @Moro It is different question. Thanks – Saba Malik Jun 25 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 – Joe Jun 26 at 7:03
  • Thank you Joe, I got the solution. – Saba Malik Jun 26 at 9:40
-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

  • 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 Jun 26 at 9:39
  • Hi@SabaMalik it would be great if you can share the code where you have used LIST. – Sandeep Srivastav Jun 27 at 7:50

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