I'm automating the execution of test cases using Java and Selenium WebDriver. Below is the scenario:
There is a page named 'Products' and When I click on 'View Details' in the 'Product' page, a popup (modal-dialogue) containing the details of the items appears.
On clicking the 'Close' button in the popup, the popup closes and the page refreshes automatically (the page is just reloading, the contents remain unchanged).
After closing the popup, I need to click on the 'Add Item' button on the same page as part of the test. But, when WebDriver is trying to find the 'Add Item' button, and if the internet speed is good, then webdriver can find and click the element.
But in case the internet speed is bad or slow, then WebDriver finds the button before the page is refreshed, but right when webdriver clicks on the button, the page gets refreshed and StaleElementReferenceException occurs.
Even if different waits are used, all the wait conditions become true (since the contents in the page are the same before and after reloading) even before the page is reloaded and StaleElementReferenceException occurs.
The test case works perfectly if Thread.sleep(); is used before clicking on the 'Add Item' button. But that's not correct usage. Is there any other workaround for this problem?