0

I am trying to use javascript executor to sendKeys for the given xpath but it's not working on browser IE 11.Since this is an hidden xpath iam using the javascript executor.I am using Eclipse tool with java.

Currently using IEDriverServer_Win32_3.14/IEDriverServer.exe and Already tried with IEDriverServer_Win32_3.12 /IEDriverServer.exe

Code trials:

  static JavascriptExecutor jse = (JavascriptExecutor)driver;
     By AAA = (By.xpath(""));
     jse.executeScript("argument[0].value='XXX';",AAA);
     jse.executeScript("AAA.value='XXX'");

Error:

org.openqa.selenium.JavascriptException: Error executing JavaScript
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'XXXX', ip: '172.16.68.66', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:46956/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 2594ae9b-69db-4f48-8e03-29d90e58dcfa
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:480)
    at xplocators.productonelocators.pOneTwoxp(productonelocators.java:85)
    at synergy.synergy_artifact.BasesynergyTest.addproductones(BasesynergyTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

Manual Steps:

  1. Login into the URL
  2. Fill up the product A
  3. Click on add product
  4. Fill up Product B
  5. Note:Unless add button is clicked product B will not show up.
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
raman san
  • 11
  • 1
  • What is your exact _Manual Step_ which you are trying to _Automate_? – undetected Selenium Jul 31 '19 at 16:16
  • 1.Login into the URL,2.fill up the product A,3.Click on add product 4.Fill up Product B ,Note:Unless add button is clicked ,product B will not show up. – raman san Jul 31 '19 at 17:00
  • Please try to use F12 dev tools in IE to check if there's any error in console. You could also check if you have got the element correctly. There're [some examples](https://www.programcreek.com/java-api-examples/?api=org.openqa.selenium.JavascriptExecutor) about how to use `org.openqa.selenium.JavascriptExecutor`, you could refer to it. – Yu Zhou Aug 01 '19 at 05:31

2 Answers2

0

In the line below, the extra parenthesis are not needed. Also, you are assigning a By locator to AAA, not a WebElement. JavaScript doesn't have a concept of the By class, only an element.

By AAA = (By.xpath(""));

This should be

WebElement AAA = driver.findElement(By.xpath(""));

With the changes above, this line should now work

jse.executeScript("argument[0].value='XXX';", AAA);

This line will not work because AAA is not defined in the JavaScript scope. But... it shouldn't be needed because the above line should do the same thing.

jse.executeScript("AAA.value='XXX'");

So, with all the changes, the final code should look like

static JavascriptExecutor jse = (JavascriptExecutor)driver;
WebElement AAA = driver.findElement(By.xpath(""));
jse.executeScript("argument[0].value='XXX';", AAA);
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

You need to take care of a couple of things:

  • If you are using Selenium v3.12.0 you need to use InternetExplorerDriver v3.12.x binary as InternetExplorerDriver v3.14.x may be incompatible.
  • Your JDK version is 1.8.0_101 is pretty ancient and you need to upgrade JDK to recent levels JDK 8u212.
  • Presuming you are trying to sendKeys() for product A right after accessing the url using executeScript() you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].setAttribute('value','XXX')", element);
    

You can find a relevant discussion in Using JS to enter text, but if I input text in one text box, the value already entered is getting deleted

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352