0

Im testing on a site that sometimes doenst really have a good connection. Everything works great until it decides to not work properly. I am seperating every element with a wait.until, everything works normally until the connection gets slow and it believes the element is present and decides to use it.

public static WebElement login_btn(WebDriver driver, WebDriverWait wait) {
    wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.id("btnEntrar"))));
    wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(By.id("btnEntrar"))));
    element = driver.findElement(By.id("btnEntrar"));
    return element;
}

I cannot find a way to simulate this until the website becomes terribly slow and I get to see if it works or not. After months of trying I am unable to find out a way to completely wait for the element to be present and not receive a staleElementReferenceException or the site actually going into an error page due to me using an element. Using the site manually without these tests cannot simulate what is happening with the webdriver. Are there any hints or suggests on what I could do?

SunnyH
  • 67
  • 11

1 Answers1

0

There are a few good answers you can try in this question and this question. One is to use Chrome dev tools to throttle adjust the download speed, https://stackoverflow.com/a/34728002/2386774. Another is Charles Web Debugging Proxy.

To solve this issue more simply, my suggestion is to specifically wait for the page to load completely then scrape the page. This will allow you to wait once and then you won't have to add waits everywhere. You would need to add additional waits if your page is dynamic, e.g. you click a button and it updates part of the page. In that case, add a wait when the button is clicked and you're good again. No need to add waits everywhere.

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • I will try these to test. Thank you very much! – SunnyH Dec 20 '16 at 16:27
  • EDIT: I already wait for the page to load once in the beginning but I am dealing with a lot of dynamic elements that update as I input into them or select others. While it waits sometimes it seems to return as present and loaded but a few milliseconds later its back to stale. Im guessing its how the site is coded to retrieve info to update it maybe. – SunnyH Dec 20 '16 at 16:28
  • One trick you can use to help with really dynamic pages is to wait for an element to be stale. For example, there's an element on the page that you want to update and then get the updated info. The problem is that if you grab the element before it updates, you get the old info or you get a stale reference. The trick is to wait for the element to go stale then wait for it to be visible. – JeffC Dec 20 '16 at 17:32