Selenium and StaleElementReferenceException - selenium

I am using selenium 3.9.1 and java to automate testing of a web application. The web application has some dynamic content based on pressing of a button for example. The page refreshes whenever this button is clicked. A java script runs on button click and updates the DOM I think. At this time, when I try to access the button (which is visible on the page), I get a staleElementReferenceException.
Does Selenium automatically reload the DOM once it is changed? I am relatively new to selenium. I have researched into this and I have tried to refresh the page using driver.navigate().Refresh() to try to see whether this will solve the problem. It does not solve the issue.
Any pointers will be deeply appreciated.

Since the page has been refreshed, the button reference you have is to the button on the old page that no longer exists.
I'd say you need to get a new reference to the button on the refreshed page (eg call FindElementById).

If the page is refreshed all the items in the DOM are now stale. What this means is that all items found before the button press will have to be found again. Any attempts to use those items will more than likely be treated with a stale element exception.
However, if the button click mearilly affects items on the page without having to ask the webserver to give you a new page you could interact with the old items.

You could do something like this:
public void SaveAndAgainClick() throws Exception{
try{
clicksaveButton(); //method to click save button
WebElement someValue = driver.findElement(By.xpath("(//input[#name='someValue'])[1]"));
someValue.click();
}catch (StaleElementException e){
WebElement someValue = driver.findElement(By.xpath("(//input[#name='someValue'])[1]");
someValue.click();
}
}
If findElement gets staleElementError while looking for (//input[#name='someValue'])[1] then it will again try one more time in the catch block and most certainly find the element and clicks on it. Your test will pass if you follow this approach.

Here are the answers to your questions :
A java script runs on button click and updates the DOM I think : If you inspect the HTML of the element through Development Tools / Inspect Element the element attributes will reveal it all.
Consider the following HTML :
<input value="Click me" onclick="alert('Click!')" type="button">
In the given HTML as per the onclick attribute of this element, if you invoke click() method on the WebElement, an alert would be generated. Similarly the onclick attribute may invoke a JavaScript or Ajax which may bring-in/phase-out new/old elements from the HTML DOM
At this time, when I try to access the button I get a staleElementReferenceException : In this case you should induce WebDriverWait for the WebElement to be interactive before attempting to interact with the element. Else you may face either of the following exceptions :
StaleElementReferenceException
WebDriverException
ElementNotInteractableException
InvalidElementStateException
Does Selenium automatically reload the DOM once it is changed? Short answer, Yes it does.
Refresh the page using driver.navigate().refresh() : No invoking driver.navigate().refresh() wouldn't be a optimum solution as it may not invoke the intended JavaScript or Ajax properly. Hence the intended WebElement may not be interactive in a optimum way.

Related

Not able to locate web element on chrome browser's settings popup

I am trying to clear browser cache, for which i need to click on clear data button of browser setting popup, but, i am not able to write xpath for the button on chrome browser
i have tried inspecting the element to find out if the button is on a iframe but its not in iframe, so i have decided to try it with an with out iframe snippet, either of ways the element is not traces out in dom
public void clearBrowserCache() throws InterruptedException{
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(2000);
System.out.println(driver.getWindowHandles());
String windowIds=driver.getWindowHandle();
// driver.switchTo().frame(windowIds);
driver.findElement(By.cssSelector(
[id=clearBrowsingDataConfirm]")).click();
}
Expected is that i should be able to click on the clear data button
Actual is that i am not able to find out the xpath for of the emlement
Depending on which version of chrome you are using, this could work:
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
However the /deep/ combinator is deprecated, so it may not work on newer Chrome's versions.
I answered how to reach inside the Shadow DOM in an other question.
You can read the whole thing at the link, but the basics are you create a "starting point" WebElement at the Shadow DOM via JavaScript, then all future look-ups reference it:
WebElement button = startingPoint.findElement(By.cssSelector("..."));

Selenium WebDriver Explicit Wait intermittently not working

Good day everyone,
I need your help in this method. I have a web page that will have a loading screen UI when the page loads, and I'm waiting for it to finish before clicking a button.
Here is my code:
#Step("Go to Audit Inquiry Screen")
public void launchAuditInquiry(){
WebDriver webDriver = Driver.webDriver;
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading-container")));
WebElement auditInquiryBtn = webDriver.findElement(By.linkText("Audit Inquiry"));
auditInquiryBtn.click();
}
My issue is sometimes, this code works fine. It will wait for the loading ui div to be invisible before clicking the button. But sometimes it will produce this error:
Error Message: org.openqa.selenium.WebDriverException: unknown error: Element <a class="module-item" href="/audit/inquiry">...</a> is not clickable at point (822, 436). Other element would receive the click: <div class="loading-container" style="display: flex; opacity: 0.899842;">...</div>
I tried adding another explicit wait before clicking the button to be sure, like this:
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading-container")));
WebElement auditInquiryBtn = webDriver.findElement(By.linkText("Audit Inquiry"));
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Audit Inquiry")));
auditInquiryBtn.click();
But it will sometime produce the same error above, and sometimes it will work fine.
I'm just confused on how to remediate the issue.
Thank you guys for the comments, especially this: Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click
It was helpful, but some items there, I have already tried, but did not work as well. The part that I did check was one item there and also a comment here also which is to force a click:
new Actions(driver).moveToElement(auditInquiryBtn).click().perform();
But I'm having second thoughts on this because, a scenario may happen that when the loading container div is still overlaying the page, then I forced clicked the submit button, it will also produce another loading container div, and I'm not sure what will happen if there are two loading container div present.
Now, my solution on this is to adjust the sleep timer of the wait function:
WebDriverWait wait = new WebDriverWait(webDriver, 10, 2500L);
It now works because it gives the loader div time to generate before the first check of wait. 500 ms was a bit fast for the loader to render. I'm still testing this but if it didn't work, I might do the solution above.
Thanks again.
First thing to try is remove the invisibilityOfElementLocated wait and just use elementToBeClickable. I've never really trusted what Selenium considered "visible" and "invisible".
I've had issues in the past where the element to click on was completely off screen, so Selenium automatically scrolled until it was considered in the viewport. But because of a floating footer, it didn't scroll enough and was still behind the footer so could not be clicked on. It was still considered "visible" because it was in the viewport.
But, if you're sure, you can try forcing a click at a coordinate instead of an element.
new Actions(driver).moveToElement(auditInquiryBtn).click().perform();

Click method is not working when using JavascriptExecutor in Selenium

I am clicking on the text box displayed on first page
WebElement txtBox = driver.findElement(By.xpath("---xpath---"));
txtBox.click();
Then after some block of execution I am getting the same textbox in new page on same window.
Here also I want to click on the text box.
I used JavascriptExecutor to do this scripting.
((JavascriptExecutor)driver).executeScript("arguments[0].click();", txtBox );
But while running the script I am getting an error message saying:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
You need to execute driver.findElement() every time you reload the page.
This method returns WebElement which is integral part of current page. If you refresh browser or navigate to some other URL or even if element is deleted and attached again by some javascript on same page, previously found element cannot be used anymore.
Here you have official explanation of this exception: http://www.seleniumhq.org/exceptions/stale_element_reference.jsp
Stale Element exception comes after you want to interact with an element loaded previously. If you get webelement and then reload the page it gives this exception because it is not in a newly created page. It is better to click the web element without assigning it to a variable like:
driver.findElement(By.xpath("---xpath---")).click();

Selenium-Webdriver (Java) failing to execute 'hoverover and click' function consistently

I'm trying to access a screen in an application, which appears when I do a mouse-hover over a tab and click on one of the options. I used Actions method to execute this using selenium. Here's my code:
element=driver.findElement(By.id("tab"));
Actions hoverover=new Actions(driver);
hoverover.moveToElement(element).moveToElement(driver.findElement(By.id("menu"))).click().build().perform();
When I login to the application and directly call this tab, I'm able to access this without any issue. But the problem occurs when I access this tab from a different screen in the application.
Whenever I access the hover-over page from a different page in the application, sometimes the page loads correctly but most of the time it fails and I recieve 'no such element' or 'stale element reference' error.
I'm really not sure how it is able to access the tab without any issue sometimes and how sometimes it is throwing errors. Please guide me here and let me know if there is anything else(any additional functions/or an alternative to Actions?) I can do so that the mouse-over click works all the time.
EDIT: I tried using both Explicit and Implicit waits and even thread.sleep as well, but in vain. In Chrome(only in chrome) when i do a manual screen refresh while it tries to access the tab, it works. But when I do the same in my code [driver.navigate().refresh()], it's not working!!
The stale element exception is probably occurring between when you set element and when you hoverover. Selenium does something like:
WebElement element = driver.findElement(By.id("tab"));
// "element" has been set
Actions hoverover=new Actions(driver);
// Between here and hoverover, "element" has changed on the DOM
hoverover.moveToElement(element).moveToElement(
driver.findElement(By.id("menu"))).click().build().perform();
// Uh-oh, what's "element?" Better throw an exception!
Try eliminating the element= line and moving driver.findElement to the inside of moveToElement().
Actions hoverover = new Actions(driver);
hoverover.moveToElement(driver.findElement(By.id("tab")))
.moveToElement(driver.findElement(By.id("menu"))).click().build().perform();
You could also try throwing in a WebDriverWait between hovering over tab and menu.
hoverover.moveToElement(driver.findElement(By.id("tab"))).build().perform();
new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("menu")));
hoverover.moveToElement(driver.findElement(By.id("menu"))).click().build().perform();

Selenium WebDriver. After waiting for the element click() freezes test

I need to click on the dynamically generated element using Selenium WebDriver (Java API).
I'm waiting for this element to appear with WebDriverWait and then clicking on it. This click succeeds but the following click on the different static element freezes the whole test. Here is the code:
webDriver.get(alfrescoURL + "/share/page/create-document");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement documentTypeList = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_documenttype-selected-form-button-button"));
documentTypeList.click();
WebElement listItem = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("yui-gen100")));
listItem.click();
// Choosing to create in new project
WebElement projectLink = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_projecttype-entry1"));
projectLink.click();
documentTypeList.click() opens a drop-down list, listItem.click() chooses an item, projectLink.click() makes a choice in the group of radiobuttons. Test silently freezes on projectLink.click(). It looks like this click() infinitly waits for page reloading that happens by some reason while it shouldn't. (Disappearing of the list after choosing an item is made by javascript that doesn't make any AJAX requests.)
I think there is something about click() blocking i don't understand. It says in it's javadoc that it attempts to block only if it causes a page to load. Nevertheless here i get a block for some reason.
If i insert a thread sleep before projectLink.click() then test works fine. It agrees with a hypothesis that i get a infinite block on click().
Thanks in advance.
I've run into this before where the test runs faster than the drop down can contract and can't click the following element. Instead of using arbitrary sleeps (although in rare cases they are necessary), can you put in a wait for a class change in the drop down?
For example, if I want to wait for the drop down to contract before moving on, I'll wait for the class of the select to change from "active" to "closed". This, of course, assumes your HTML has these dynamic classes in place.
Another possibility is to set an implicit wait, giving yourself enough padding for instances like these:
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
I would suggest that you try other click options too:
a) Actions#click()
b) Javascript click()
If any of those click works then it means the issue is with the selenium's WebElement Click method which needs to be reported.