AndrasCsanyi
AndrasCsanyi

Reputation: 4255

proper jQuery code is not working in webdriver

I'm migrating our Selenium test to WebDriver and there is a test which use jQuery to identify how much element are in a certain place. This code is working fine with Selenium1 but it is not working with WebDriver. When I say that 'it is not working' I mean that it returns a very big and ugly null instead of the desired 3 as string. However, If I run it with Selenium1 than I got the desired 3 as string.

I googled around but I haven't find anything which could help to solve this issue.

The examples are not the real code, they are just examples. If I run the original code in firebug than it always works fine.

The html code:

<div id='listOfElements'>
  <div class='items'>
    <ul class='items'>
        <li class='listedElements'>elem 1</li>
        <li class='listedElements'>elem 2</li>
        <li class='listedElements'>elem 3</li>
        <li class='listedElements'>elem 4</li>
    </ul>
  </div>
</div>

The javascript code:

window.$("div[id='listOfElements']>div[class='items']>ul[class='items'] li[class*='listedElements']").length

The c# code:

public string GetEval(string script)
{
        var js = _driver as IJavaScriptExecutor;
        return (string) js.ExecuteScript(script);
}

This method always returns with null, however I require the amount of the "listeElements".

Versions: c# 3.5

Webdriver: according to my filesystem I downloaded it around start of March this year and I don't know how can I get the revision number of it.

jQuery: 1.6.2

Thanks in advance for any help!

Upvotes: 0

Views: 1007

Answers (1)

Scott
Scott

Reputation: 9488

I think you need to return the value, so if the above code is correct:

return window.$("div[id='listOfElements']>div[class='items']>ul[class='items'] li[class*='listedElements']").length;

You simply need to add the return portion to the front of the string you are to execute.

Upvotes: 1

Related Questions