2

I am at my wits end with this and I'm sure the answer is simple but I just can't figure it out.

I've got a jquery selector that finds and element perfectly when I put this in the console of google Chrome:

$(".answer__label:contains('Yes')")

Now I want to use this to retrieve an element in a WebDriver test, so I'm using JavascriptExecutor in the following way:

private WebElement findByJSText(String text) {
    String script = String.format("return ('.answer__label:contains('%s')')", text);
    WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript(script);
    return element;
}

This gives the error, "missing ) in parenthetical". I've seen references to this error all over, but none that seem to apply to my situation.

How do I massage this to return the element properly?

(please don't respond telling me to use driver.findElement(By.linkText), thanks)

ADDENDUM

Here's what my final working method looks like:

private WebElement findByJSText(String text) {
    //String script = String.format("return $('.answer__label:contains('%s')')", text);
    String script = String.format("return $(\".answer__label:contains('%s')\")[0]", text);
    WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript(script);
    return element;
}

I may add a 2nd parameter for a css string to make this more flexible.

Thanks all for the help!

1 Answer 1

3

In this line:

String script = String.format("return ('.answer__label:contains('%s')')", text);

Your javascript code is error, where is the jquery $ function? Correct js code should be:

String script = String.format("return $('.answer__label:contains('%s')')", text);

Edit:

You have the ' within another '. Thats the problem, I tried the following js code and its working fine for me:

String script = String.format("return $(\".answer__label:contains('%s')\")", text);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for catching that. I've edited it so many times I must have trounced it somewhere along the line. ;) This still gives me the "Missing ) after argument list" error, however.

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.