3

I am working on selenium webdriver and I need to enter text without using sendkeys method, because the search popup text field is hidden. So I wrote below code:

1st way:

((JavascriptExecutor)driver).executeScript("document.getElementByXpath('//input[@class='form-control input-small input-inline']').value='TextValue'");

2nd Way:

JavascriptExecutor jse = (JavascriptExecutor) driver;
//jse.executeScript("document.getElementByXpath('//input[@class='form-control input-small input-inline']').value ='abcd';");

3rd way:

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].type ='search';",chemObject.getSearchPopup());

but I am getting syntax error as I mentioned in the title.

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Manali
  • 31
  • 1
  • 1
  • 2

1 Answers1

7

Your first one is producing the JavaScript error, and the reason is mixed-up single quotes, which you wouldn't have seen from the Java code, not until the JS was executed.

The simplest fix is to replace:

((JavascriptExecutor)driver).executeScript("document.getElementByXpath('//input[@class='form-control input-small input-inline']').value='TextValue'");

with:

((JavascriptExecutor)driver).executeScript("document.getElementByXpath(\"//input[@class='form-control input-small input-inline']\").value='TextValue'");

However, it still won't work for you (nor your second attempt), because getElementByXpath isn't a JavaScript DOM method.

This is all covered in a very similar thread here.

Community
  • 1
  • 1
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73