Executing the executeScript method in Microsoft Edge using Java and WebDriverWould you like a different version?

I'm currently attempting to execute the following code in Microsoft Edge using WebDriver

ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return 
document.readyState").toString().equals("complete");

JavascriptExecutor js = (JavascriptExecutor) getDriver();
boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");

An exception is being thrown:

Exception class:org.openqa.selenium.JavascriptException

The issue stems from:

org.openqa.selenium.JavascriptException: javascript error: Function is not a constructor

We've configured EdgeOptions as follows

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setBrowserName("MicrosoftEdge");
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("prefs", edgePrefs);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.merge(desiredCapabilities);
edgeOptions.setPageLoadStrategy("eager");
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
edgeOptions.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
driver = new EdgeDriver(edgeOptions);

Any recommendations or suggestions?

Answer №1

executeScript()

Executing JavaScript within the current frame or window context is the purpose of this method. The provided script fragment will be executed as an anonymous function's body. In the script, 'document' can be used to refer to the current document. Keep in mind that local variables won't be accessible once the script finishes running, while global variables will persist. If the script has a return value (i.e., contains a return statement), the following actions are taken:

  • When dealing with an HTML element, this method returns a WebElement
  • If the result is a decimal, a Double will be returned
  • For non-decimal numerical values, a Long is returned
  • A boolean results in a Boolean response
  • All other cases lead to a String being returned.
  • In the case of an array, a List is returned with each object following the mentioned rules. Nested lists are supported.
  • In the case of a map, a Map<String, Object> is returned with values adhering to the specified rules.
  • If the value is null or there is no return value, then null is ultimately returned.

The arguments accepted must be a number, boolean, String, WebElement, or a List containing any combination of those types. An exception will occur if the arguments do not meet these requirements. The arguments are then passed to the JavaScript through the "arguments" magic variable, simulating a call via "Function.apply".


This Scenario

If your scenario involves waiting for document.readyState to reach complete, you can achieve this using WebDriverWait in the following manner:

new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error occurred due to an improperly formatted authorization header while attempting to upload an object to S3 using the JavaScript SDK

When attempting to upload an object to Amazon S3 using their JavaScript SDK, I encounter the following error message: <Error> <Code>AuthorizationHeaderMalformed</Code> <Message>The authorization header is malformed; the reg ...

The call is not being answered by the server route (NodeJS + express)

I have encountered an issue while setting up a server using NodeJS and Express. When I attempt to make a get request to the basic route ('http://localhost:3000/'), the request seems to hang indefinitely. Despite thoroughly reviewing my code multi ...

Setting a default value within an input tag: A step-by-step guide

const [userData, setUserData] = useState([]); const handleUserInfo = (id) => { fetch(`https://602e7c2c4410730017c50b9d.mockapi.io/users/${id}`) .then(res => res.json()) .then(data => setUserData(data)) } <inpu ...

I am looking to incorporate a new row into a table that features a + icon using Selenium with Java

I need help with my qualification table setup. The first entry is mandatory and cannot be deleted, but I want to add more rows for additional qualification details. Refer to the image description I have attached. I am new to automation and would appreciate ...

Tips for implementing event handlers on dynamically generated li elements in VueJS

Creating multiple ul elements using v-for in the following way <div v-for="item in info"> <ul> <li><a>{{item.number}}</a></li> <li><a>{{item.alphabet}}</a></li> </ul> </div&g ...

JQuery / Javascript - Mouse Position Erroneously Detected

I'm currently working on developing a drawing application where users can freely draw by moving their mouse over a canvas. My goal is to create a pixel at the precise location where the user drags their mouse. However, I've encountered an issue ...

What steps can be taken to update the cart if all products have been removed?

How can I implement a refresh for my cart page every time the product length is 0 without causing unreachable code? It seems like there must be a simple solution to this problem. switch (action.type){ case "REMOVE_PRODUCT": return ...

javascript, issues with floating and displaying elements

I am currently working on creating a JavaScript menu that has a click function to smoothly slide up and down. Although the JavaScript code appears to be functioning correctly, it seems that there is some interference with the CSS. Despite attempting vario ...

Intercepting HTTP responses to handle headers

I'm facing an issue where I am trying to retrieve a custom header sent from the server in my $http response interceptor. However, the only header that is accessible is the Content-type header. How can I troubleshoot this problem? Here is part of my $ ...

Having trouble accessing the dashboard after uploading a document to Firestore

I am currently working on a project where I need to upload an audio file to Firebase storage and also add document data related to the audio file in Firestore database. The process involves recording the audio, uploading it to Firebase storage, submitting ...

Simple steps to trigger a PHP page from a JavaScript page by utilizing express functions

Currently, I am attempting to showcase a PHP page using JavaScript with express functions. Here is an illustration of the code: var express = require('express') var app = express() app.get('/', function (req, res) { res.send(' ...

Navigating the Terrain of Mapping and Filtering in Reactjs

carModel: [ {_id : A, title : 2012}, {_id : B, title : 2014} ], car: [{ color :'red', carModel : B //mongoose.Schema.ObjectId }, { color ...

The TestNG Report seems to have a limitation of showing only up to 21 tests

I have a project using Maven which runs TestNG tests through a Testng xml. The XML file includes the following listeners: <listener class-name="org.uncommons.reportng.HTMLReporter" /> <listener class-name="org.uncommons.reportng.JUnitXMLReporter ...

Tips for uploading images in Next.js using Firebase

While following a tutorial on Next.js, I encountered an issue with the outdated version of Firebase being used. Despite trying different solutions from the documentation and various sources, I am still facing an error message while attempting to upload ima ...

Use geb to upload the file

Hello, I am trying to upload a file during my test using Geb. The instructions in the Geb-book (http://www.gebish.org/manual/0.9.0-RC-1/navigator.html#file_upload) are not working for me. My HTML code includes a file input field: <input id="bla-bl-bla ...

The useEffect hook in React is signaling a missing dependency issue

Any tips on how to resolve warnings such as this one src\components\pages\badge\BadgeScreen.tsx Line 87:6: React Hook useEffect has a missing dependency: 'loadData'. Either include it or remove the dependency array react-hoo ...

What is the method to display just the final 3 characters within a paragraph?

Is there a way to display only the last three characters of a string using either a method or a string method? I consistently have a 13-digit number, but I specifically require showing only the final three digits. Appreciate any assistance provided. Than ...

Scraping tables with Python Requests and Selenium can be a challenging task

Here is the link to the website I am referring to: I am looking for a way to extract and store the tables labeled 'AUCTION SPECIFICATIONS & RESULTS' that appear on the jao.eu/auctions# page after manipulating the selections in the OUT AREA, ...

A helpful tip on incorporating Snack Bar Material UI within an if statement

When my Camera Component encounters an error, I want to display a snackbar notification. I attempted to encapsulate the component within a function and pass the error message as props, calling it in the if statement. However, this approach did not work as ...

Having trouble choosing multiple options from autocomplete drop-down with Selenium web-driver in Python

I am currently in the process of automating a webpage built with Angular that features an auto-complete dropdown with numerous elements. My goal is to click on each individual element and verify if it populates all the fields below accordingly. Below is th ...