FirefoxDriver webdriver.load.strategy unstable findelements getting elements from the wrong page

此生再无相见时 提交于 2019-12-02 06:57:09

问题


I am using FirefoxDriver in an application that rapidly moves through several similar but not identical pages. To speed up the execution (I need to use FF), I set the webdriver.load.strategy property to 'unstable.' This does indeed speed things up by not completely loading pages, but I found a very strange error.

In examining the file with the list of pages and the data on them, I found that some pages were matched up with the wrong data. When I debugged, everything worked fine while stepping, but as soon as I let the program run for even a few pages, it started getting data from the wrong page again. (To clarify, e.g. if I do a driver.get(www.google.com) , followed by driver.get(www.stackoverflow.com), and then do driver.findElements() to get StackOverflow's page title, it will return "Google.")

When closely watching the browser running, it appeared to me that there was a mismatch between the urls in the url box and the pages actually being displayed. The urls were changing much faster than the page. I suspect that what is happening is that the driver is not waiting until the page is fully loaded before calling findElements and therefore gets elements from the previous page, which have the same class names.

This makes some sense given what webdriver.load.strategy 'stable' is supposed to do, but I set a Wait on an element on the page and it doesn't seem to be waiting. Can it be because every page has the same element, so the element was visible already? I can't wait on anything else because all the pages have the same setup - it's only the individual text that's different, and I don't know what that will be in advance.

Has anyone encountered this problem? Does my assumption about what's causing the problem correct? Is there anything I can do about it short of removing the unstable load strategy?

Thanks, bsg EDIT I am adding some code, even though the code works perfectly when webdriver.load.strategy 'stable' is not set.

for(String url : urllist)
{
  driver.get(url);
  WebElement header = (new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.tagName("h1")));
  elements = driver.findElements(By.className(elementclassname));
}

Note that there is an h1 and several elements with class=elementclassname on each page.


回答1:


I suspect that what is happening is that the driver is not waiting until the page is fully loaded before calling findElements and therefore gets elements from the previous page, which have the same class names.

I think your hypothesis is correct.

Pretty much the same is written in the description of unstable strategy:

There is beta feature to make firefox not wait for the full page to load after calling .get or .click. This may cause immediate find's to break, so please be sure to use an implicit or explicit wait too.

As a (not quite good-looking) workaround, you may refer driver to a page which does not contain the element in the presenceOfElementLocated() (e.g., blank page).

Something like:

for(String url : urllist)
{
  driver.get(url);
  WebElement header = (new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.tagName("h1")));
  elements = driver.findElements(By.className(elementclassname));
  driver.get("about:blank"); // <<<<<<<<<<
}

So, when driver gets to a new url, there will be a blank page and it will need to wait for an element to appear on the page.



来源:https://stackoverflow.com/questions/20954605/firefoxdriver-webdriver-load-strategy-unstable-findelements-getting-elements-fro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!