Home > Software design >  javascript error: Cannot read properties of undefined (reading 'element')
javascript error: Cannot read properties of undefined (reading 'element')

Time:08-03

I am trying to run the following code:

WebElement body = driver.findElement(By.xpath("/html/body"));
js.executeScript("var injector = window.angular.element(argument[0]).injector(); var $http = injector.get('$http'); return ($http.pendingRequests.length === 0);",body);

I however get the following error:

org.openqa.selenium.JavascriptException: javascript error: Cannot read properties of undefined (reading 'element')
  (Session info: chrome=103.0.5060.134)
Build info: version: '4.2.2', revision: '683ccb65d6'
System info: host: 'DESKTOP-UPVJF2U', ip: '192.168.0.132', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '18.0.1.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [c6b4ce36aa115d9746065cead6c3271b, executeScript {script=var injector = window.angular.element(argument[0]).injector(); var $http = injector.get('$http'); return ($http.pendingRequests.length === 0);, args=[{ELEMENT=a0c33e06-9db9-49ed-984f-535b6bc72f7a, element-6066-11e4-a52e-4f735466cecf=a0c33e06-9db9-49ed-984f-535b6bc72f7a}]}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 103.0.5060.134, chrome: {chromedriverVersion: 103.0.5060.134 (8ec6fce403b..., userDataDir: C:\Users\win10\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:53279}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:53279/devtoo..., se:cdpVersion: 103.0.5060.134, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: c6b4ce36aa115d9746065cead6c3271b
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:588)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:522)
    at com.vilcart.app.CustomerCreation.testCreation(CustomerCreation.java:118)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
... Removed 28 stack frames

Can you please let me know how i could resolve this issue?

CodePudding user response:

Since you want to get arguments list element by index try to replace

argument[0]

with

arguments[0]

CodePudding user response:

@suhail shaik You can try the following, assuming you want to check if all the pending $http requests are served by Angular in the background.

  String angularReady = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";

  boolean isAngularReady = (boolean)js.executeScript(angularReady);   
  • Related