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?

1

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

6 Comments

For using RenderedWebElement interface which package should be imported in Java?
It might have changed to WebElement
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?
previous code: jse.executeScript("$('#gbqfq').click();"); According to your direction current code is: jse.executeScript("jQuery('#gbqfq').click();"); Am I right?
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?
|
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();

1 Comment

Among all the above answers, this is the only one that works. Thanks mate!
0

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

3 Comments

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?
I am not aware of jQuery and I suppose there is no such automatic method from selenium
I need an automatic approach for identifying elements in jQuery, which can be "converted" to Selenium elements / locators.

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.