0

I'm trying to set a field value using javaScript with the following code in Selenium + Java

    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("document.getElementByXpath(\"//*[@tabindex='17']\").value = '05741180';");

But, I'm getting this exception:

org.openqa.selenium.WebDriverException: unknown error: document.getElementsByXpath is not a function

Any idea what I'm doing wrong?

3
  • 1
    getElementByXpath is not a member of document Commented Mar 7, 2017 at 20:36
  • So, how can I set value of a field using xpath in this case? Any Idea? Commented Mar 7, 2017 at 20:46
  • developer.mozilla.org/en-US/docs/… looks like what you need Commented Mar 7, 2017 at 21:01

1 Answer 1

1

If you want to enter value '05741180' in the field recognized by xpath '//*[@tabindex='17']', you can do that using javascript in the following way:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].setAttribute('value', arguments[1])", driver.findElement(By.xpath("//*[@tabindex='17']")), "05741180");

Let me know, if you have any further queries.

Sign up to request clarification or add additional context in comments.

2 Comments

No, it did not work. When I execute it, the field is not filled and I get no error message :-( Note: I'm executing it on chrome
Anyway, your message helped me to find this solution which worked just fine: JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("arguments[0].value = '05741180'", driver.findElement(By.xpath("//*[@tabindex='17']")));

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.