7

This is the situation:

  • I use the Java API of Selenium 2 to open and control a firefox browser instance
  • I load the jQuery script to a page via JavaScript execution
  • I then use jQuery expressions to select elements and traverse through the DOM tree

Now is my question, can i somehow find a unique identifier for each of the found elements? My goal is to get the same element with Selenium by using an Xpath or CSS selector. So it would be most straighforward if i could generate an unambiguous selector for the elements in jQuery. Other ideas are welcome too.

I need an automatic approach for identifying elements in jQuery, which can be "converted" to Selenium elements / locators.

/edit

To make it clearer:

If i have selected an element in jQuery:

webDriver.executeScript("var element = $('#myDiv input.test')");

Now, I want something like this:

WebElement webElement = webDriver.executeScript("return element");

Is that possible?

Alp
  • 29,274
  • 27
  • 120
  • 198
  • 1
    This is basically the other direction to this question: http://stackoverflow.com/q/5605456/675065 – Alp Apr 09 '11 at 14:45

3 Answers3

11

I found the solution, which is quite easy:

String jQuerySelector = "'#myDiv input.test'";
RenderedWebElement webElement = (RenderedWebElement) ((JavascriptExecutor) webDriver).executeScript("return $(" + jQuerySelector+ ").get(0);");

Working with an element in jQuery which was previosly selected in Selenium works too:

String jQuerySelector = "arguments[0]";
((JavascriptExecutor) webDriver).executeScript("return $(" + jQuerySelector+ ").doSomethingInJquery();", webElement);
Alp
  • 29,274
  • 27
  • 120
  • 198
  • For using RenderedWebElement interface which package should be imported in Java? – Ripon Al Wasim Aug 23 '12 at 08:50
  • It might have changed to `WebElement` – Alp Aug 23 '12 at 09:50
  • I am getting the following error during using jQuery in WebDriver with java: org.openqa.selenium.WebDriverException: $ is not defined (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 915 milliseconds... How can I solve this? – Ripon Al Wasim Aug 23 '12 at 09:56
  • previous code: jse.executeScript("$('#gbqfq').click();"); According to your direction current code is: jse.executeScript("jQuery('#gbqfq').click();"); Am I right? – Ripon Al Wasim Aug 23 '12 at 10:37
  • sorry to say, same problem : org.openqa.selenium.WebDriverException: jQuery is not defined (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4.29 seconds. Is there any configuration for jQuery? – Ripon Al Wasim Aug 24 '12 at 04:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15753/discussion-between-alp-and-ripon-al-wasim) – Alp Aug 24 '12 at 07:55
1

Using eval makes it even easier. Use an index selector such as [0] with the jQuery code or it will return a collection of elements.

    String elementLocator = "$('#btnID')[0]";
    public RemoteWebElement getElementByJQueryLocator(String jQueryLocator){
        JavascriptExecutor js = (JavascriptExecutor) driver;
        RemoteWebElement element = (RemoteWebElement) js.executeScript("return eval(arguments[0]);", jQueryLocator);
        return element;
    }

        RemoteWebElement webElement = getElementByJQueryLocator(elementLocator);
        webElement.click();
JohnP2
  • 1,899
  • 19
  • 17
0

Not sure of your exact problem but you can build you locator using html id, name, class etc attributes.

Tarun
  • 3,456
  • 10
  • 48
  • 82
  • I know that. But is there an automatic method that gives me always a valid locator to find an element i have selected in jQuery? – Alp Mar 31 '11 at 08:34
  • I am not aware of jQuery and I suppose there is no such automatic method from selenium – Tarun Mar 31 '11 at 09:32
  • I need an automatic approach for identifying elements in jQuery, which can be "converted" to Selenium elements / locators. – Alp Mar 31 '11 at 09:46