Home > OS >  Selenium didn't click second time after being reloaded to homepage
Selenium didn't click second time after being reloaded to homepage

Time:09-27

I have a test case here:

My page left part is an accordion menu. When I performed click parent menu, it worked. And the right part of the website will display "You do not have authority" and return me back to the homepage.

The right part will display normal content when I second visit. So I should click the menu again and I write like so in my test code. But it didn't perform any click action - just passed the click action to the next step. So my test fails and throws error.

I looked at my browser which is controlled by selenium webdriver. It is apparently frozen after the page reloads to the homepage.

The error message shows:

Test method AutomationTesting.cuteTest.CheckChocolate threw exception: 
OpenQA.Selenium.WebDriverTimeoutException: Timed out after 5 seconds ---> OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='menuChocolate']/a"}

My test code: (Why I have to click the second time is because this page needs to come the second time to show the normal UI)

  DessertsMenu.Click();

  WebDriverWait waitForMenu= new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
  waitForMenu.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@class='menuChocolate']/a"))).Click();
            
  DessertsMenu.Click();
  WebDriverWait waitAgain= new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
  waitAgain.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@class='menuChocolate']/a"))).Click();

I looked at the browser which is controlled by selenium, it actually clicks the menu. But I don't know why it didn't click again. Is there anything I am missing?

CodePudding user response:

If the page is reloaded, I think you will have to locate the parent menu again.

If, even after locating the element, you are not able to click the parent menu, then I would suggest you to try clicking the parent menu using JavaScriptExecutor. https://www.guru99.com/execute-javascript-selenium-webdriver.html

CodePudding user response:

You can troubleshoot it with below options--

  1. can you check after first click if it is showing the same xpath ,it might be possible that after clicking xpath is getting changed
  2. you can put some wait like one second between first and second click

3.If above two options are not working then you can use JavaScriptExeccutor

  • Related