Is it possible for Selenium's JavaScript Webdriver to interact with global JavaScript variables?

Looking to access global variables using JavaScript with WebDriver.

Here is my code snippet:

this.Then(/^I read global var$/, function (selectedElement) {
    fetchGlobalVar(window.location.href);
});

function fetchGlobalVar(varName){
return varName;
}

Error Message: ReferenceError: window is not defined

Answer №1

The script is currently executing on a node environment instead of a browser. Therefore, attempting to pass window.location.href to your readGlobalVar function will result in failure since the object window is not defined in this context.

If your goal is to wait until a specific URL value is reached, it may be beneficial to explore using until.urlMatches.

Answer №2

When utilizing this specific module...

The JavaScript execution environment in Node and the browser controlled by Selenium are distinctly separate. They do not share variables and communication is done through passing messages via the webdriver.

To access a variable from the currently loaded page, the execute method must be used to inject some JS into the page.

browser.execute(function () {
    return window.location.href;
}).then(function (result) {
    console.log(result.value);
});

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

Similar to Java's Robot class, C# offers the functionality of programm

I'm looking for the C# equivalent of Java's Robot class to control mouse pointer movements. Since the Actions class cannot be directly used for keyboard and mouse functions, I need a way to visually move the mouse pointer in Selenium using C#. Fo ...

Advantages of choosing between the <NextLink/> and the <Button href="/somePage"/> components in the powerful Mui React UI libraries

Currently engaged in a project, I am wondering if there exists a substantial disparity in the utilization of these two components. Prior to this change, the return button was implemented as follows: <NextLink href="/settings" ...

IE presenting problems with JQuery content loaded via Ajax

I operate a website that showcases various products, each with a corresponding link to a fancybox displaying detailed information about the product (detailed.php file). <a class="fancy fancy'.$_GET['type'].'" href="detail.php?id=&ap ...

When you click on a list of links, a stylish image gallery will appear using fancybox

Can anyone lend a hand with this? I would greatly appreciate any assistance CSS <a id="fancybox" href="javascript:;">Gallery 1</a> <br /> <a id="fancybox" href="javascript:;">Gallery 2</a> <br /> <a id="fancybox" hr ...

dividing JavaScript files in the three.js game development platform

After spending two years teaching myself unity/c#, I believe I have the coding skills to transition from an engine to a framework for better control over the environment. As I started working on first person environment features, I wanted to organize my co ...

Assistance needed for jQuery append/prepend function selector

Imagine you have the following HTML code: <div> <span></span> <span></span> <span></span> </div> Is there a way to add text before the last span tag? Here's an example: <div> <span>< ...

Ways to resolve the issue "Module Not Found Error: Cannot locate module './src/bot/index'"

Whenever I run the command node src/index.js I encounter the following error message: Error: Cannot find module './src/bot/index' Require stack: C:\Users\MIMAR\Desktop\EJS\src\index.js What could be causing this er ...

Ensuring proper execution of the next callback function in an Express get request

I am currently running an express server on nodejs with a specific file structure that I need to convert into a list of links. Included below are my recursive functions for dealing with the file structure. Here are the file structure functions: function ...

Guide for adding an image directory to a React application for deployment/testing purposes

I am currently working on a personal project and there are two aspects that have been causing me some frustration. The project is coded in React, with an express node application serving as the backend. In the frontend, I am able to upload and send images ...

several parameters for the `ts-node -r` options

Can I include multiple require statements in the package.json script before running with ts-node -r? "scripts": { "start": "ts-node -r dotenv/config newrelic src/index.ts", } I'm having trouble adding both "dotenv/config" and "newrelic" si ...

Unable to foresee the sudden appearance of the pop-up

I have encountered an issue with a popup (not a browser popup, but one within the application) that I am currently handling with a 20-second Thread.sleep. However, I am looking to decrease the wait time and handle the issue more efficiently. Can anyone pro ...

Mac users may experience lag when using Javascript parallax effects

I created a parallax page where the background image changes using JavaScript translateY(- ... px)' similar to what you see on the firewatch website. On Windows, the parallax effect works smoothly. However, on macOS it is smooth only in Safari. All ...

Tips for labeling subplots in PLOTLY JS

Looking for some guidance on adding titles to plots in Plotly JS. I've checked out the documentation but couldn't find anything helpful. Any tips or suggestions would be greatly appreciated! ...

Press the div using JavaScript and jQuery

Is it possible in JavaScript to automatically click on a hidden div once it is displayed? For instance, imagine we have a div with the style "display:none;" like so: <div style="display:none;" id="button">Hello World</div> Once this div&apos ...

Attempting to incorporate Font-Awesome Icons into the navigation bar tabs

As a newcomer to React, I've been attempting to incorporate Font Awesome icons into my secondary navigation bar. Despite using switch-case statements to iterate through each element, all the icons ended up looking the same, indicating that only the de ...

Iterating over the local storage to locate a specific item

Utilizing local storage extensively has been beneficial for maintaining state by storing the session on the client side. However, a challenge arises when updating the session. Within my code, there are multiple lists of objects that I serialize in JSON fo ...

Is the Material UI Checkbox toggled on or off?

How can I determine the status of a Metrial UI Checkbox (React Material UI Checkbox component) using Webdriver or a similar browser controller? What is the recommended approach for this specific task? Adding an event listener and an on/off attribute migh ...

How can PHP be used to decode JSON and transmit it to Javascript?

I am aiming to utilize the Twitter API in order to dynamically populate slides on a webpage with recent tweets without needing to refresh the entire page. Currently, my approach involves making an AJAX call every few seconds from a JavaScript function on ...

Creating a customized function to trigger actions when multiple Google Maps markers are clicked

I am working on creating a custom function for all the markers I loop through. The issue (I think) is that I need to create the variable name "markers" with an incrementing number or something similar. Here is my current code: $(document).ready(function() ...

Slowdown in functionality

My current project involves creating a user interface for an Oven configuration application using CSS, Java Script, Jquery & Json. While the interface performs actions within milliseconds on browsers and simulators, my client has noticed that it takes seco ...