1

I'm not sure whether is there any solution for my issue but, unfortunately I haven't found any article or information about it.

The situation is the following. We have a site which uses jQuery heavily and there is a service which refreshes a part of the site in every 5th or 10th second. Due to this half of the time I got this error from WebDriver:

"Element not found in the cache - perhaps the page has changed since it was looked up"

According to the Internet I got this error when the DOM tree has changed between the moment when the WebElement has been initialized and when I want to use it to perform, for example, a click event.

According to our developers our jquery solution has a variable which controls whether the page will be refreshed or not. So, to solve my issue I have to overwrite this variable. I have to overwrite this variable in that way the jQuery will be able to understand it - I mean in the same instance if this definition is proper in this context.

So, I would like to ask whether is possible or not? If so, than I would like to ask a small example.

Thanks in advance!

András

5
  • I can't provide a code because it is owned by our client. On the other hand, not necessary because I don't know what should contains the sample code. I think a describe what the problem is. If not, than I try it again. :) Commented Apr 24, 2012 at 14:09
  • So your question is concerned with the testcase - not with the actual implementation? Commented Apr 24, 2012 at 14:31
  • Yes, I wanted to know whether it is possible to manipulate a javascript variable via webdriver and in this way whether possible to manipulate the whole behaviour of the site. Commented Apr 25, 2012 at 8:04
  • @SayusiAndo: What programming language are you using? Java, C#, Python or what? Commented Aug 23, 2012 at 4:33
  • @SayusiAndo : I have understood you are not able to provide code as it is secret. Can you please provide similar other code (NOT your project code as you do not want to disclose it) so that it is easier to provide you the specific answer. Commented Aug 23, 2012 at 7:01

4 Answers 4

4

I can only agree with Aleh.

Using JavaScriptExecutor is the only way to handle such issues.

I had a problem with jQuery jNice library and couldn't find any other solution.

Here is an example in Java for filling a text field:

JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("document.getElementsByName('<field_name_gets_here>')[0].value='" + your_value + "'");
Sign up to request clarification or add additional context in comments.

Comments

2

If the JavaScript variable you mentioned is global, then yes - you can overwrite it by executing JavaScript from your Selenium. For example, if that variable is called doRefresh, you can overwrite it by executing JS like this: doRefresh = false; from Selenium.

If that variable is not global, the above won't work. However, it sounds like the elements in question might be dynamically loaded via ajax - in which case the xhr object is global and you can access it instead.

So, first you can make a local copy of the xhr object and then overwrite the original (effectively disabling it) by executing JavaScript from Selenium:

// create a copy of the xhr object for later use
var xhrHolder = window.XMLHttpRequest;

// overwrite the original object to disable it
window.XMLHttpRequest = {};

Then find your element via Selenium as you would normally. And proceed with your test.

When finished, you can put the xhr object back in place (so the page can continue refreshing and doing ajax) by executing JavaScript from Selenium:

// put the xhr object back
window.XMLHttpRequest = xhrHolder;

Comments

1

You can try my approach - I created my own wrapper for situations where page might be loading. The below part of code tries to search element in the loop, for three seconds (configurable). BTW the driver variable below is instance of WebDriver

private WebElement foundElement;
public WebElement find(By by){
    for (int milis=0; milis<3000; milis = milis+200){
        try{
            foundElement = driver.findElement(by);
        }catch (Exception e){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    return foundElement;

}

And later in the code:

 WebElement submitButton = find(By.id("submitNewBid"));
 submitButton.click();

1 Comment

I see the point of your solution and thank you very much, it will be useful!
0

I believe it is possible. Example for c#:

((IJavaScriptExecutor)driver).ExecuteScript("window.$('.class').data('var') = 0;")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.