Executing Scripts within the Browser: A Guide to Using Selenium

One of my current objectives involves running a script on my Selenium browser that establishes a variable and then using DevTools to access this variable in the console log.

Below is the conflicting script that I am encountering issues with:

from selenium import webdriver
from time import sleep

chromePath = 'Selenium Injection\chromedriver.exe'

driver = webdriver.Chrome(executable_path=chromePath)

driver.get('https://www.google.com')
driver.execute_script(
    "var test = 'Test Now';"
    "return test"
)

After running the script, I attempt to retrieve the value of the test variable but encounter an error.

Answer №1

In order to ensure proper access, you need to assign the variable to the window scope rather than the function scope

driver.execute_script("this.test = 'Test Now'")

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

Having trouble with submitting an Ajax form to a MySQL database

My expertise lies in PHP and HTML, but I'm currently learning JavaScript. I'm facing a challenge with creating a form that can submit data to be inserted into MySQL without reloading the page (using AJAX). Here is the form I have: <form id=" ...

Sending JSON data from an iOS app to a Flask backend

Within my Flask Python web application, I store certain parameters in SessionStorage to later send back to Flask and save this data as a text file. Interestingly, the process functions perfectly on PCs and Android devices but encounters issues on iOS devi ...

Is the Selenium WebDriver disregarding the setTimeout command?

Has anyone experienced the issue where the selenium.setTimeout() command is being ignored when using WebDriverBackedSelenium? Any insights or solutions? ...

Repeated keys in Pyparsing Dictionary (dhcpd.conf)

In an attempt to convert a dhcpd.conf file into a Python dictionary using pyparsing, the following syntax example is provided: ddns-update-style none; log-facility local7; deny unknown-clients; deny booting; subnet 10.42.200.0 netmask 255.255.255.0 { ...

Connect Promise.all() with an array of identification numbers

I'm fairly new to working with Promises and I have a question regarding linking the results of `Promises.all()` to unique IDs for each promise once they resolve. Currently, I am making requests to a remote server and retrieving data for each request. ...

I'm faced with a predicament in Python where I need to split a portion of text using the line-ending characters at the end of each

In my program, I am working on analyzing XML files and one of the tasks is to split the data into sentences. However, I have encountered an issue where my line end characters are missing. I need to include them in order to add annotations with XML tags at ...

Press the 'Export' CSV option within a SharePoint List using Selenium

To complete a CSV download from a SharePoint list, I need to click on two buttons located in the top command bar. https://i.stack.imgur.com/zTTyJ.png https://i.stack.imgur.com/Z9zB1.png Despite using the 2 locators and an explicit wait mentioned below, I ...

Tips for generating an HTML template as a string using jQuery

I have developed a dynamic modal using plain JavaScript, triggered by a button click. This modal is controlled based on the attributes `data-open-hours` and `data-closed-hours` in the HTML. If you take a look at my demo, you will notice an issue. Let me e ...

Having difficulty ensuring DayJs is accessible for all Cypress tests

Currently embarking on a new Cypress project, I find myself dealing with an application heavily focused on calendars, requiring frequent manipulations of dates. I'm facing an issue where I need to make DayJs globally available throughout the entire p ...

I possess an array with a specific structure that I wish to substitute the keys as demonstrated below

The data array is currently structured in this way. I have attempted various methods but have not been able to find a suitable solution. I need to send the JSON below via an AJAX request, however, I do not want the keys 0 and 1 in both child arrays. [sch ...

C# Troubleshooting: Dealing with Selenium XPath Error

Currently, I am facing a challenge with my HTML page while using Firefox and/or Chrome. I am attempting to determine the correct XPath of the image element that I want to click on the webpage. My coding is in C# and I am using Selenium Automation for this ...

Building a single page web application using TypeScript and webpack - a step-by-step guide

For a while now, I've been working on single page applications using Angular. However, I'm interested in creating a single page application without utilizing the entire framework. My goal is to have just one .html file and one javascript file, w ...

Enhancing nouislider jQuery slider with tick marks

I have integrated the noUIslider plugin () into one of my projects. I am seeking guidance on how to display tick marks below each value on the slider. This is the current initialization code for the slider: $slider.noUiSlider({ 'start': sta ...

Preserving state during navigation and router refresh in Next.js version 13

In the component below, we have a Server Component that fetches and renders data. When router.refresh() is called on click, it reruns the page and refetches the data. However, there is an issue with Nextjs preserving the state. Even though the server compo ...

Launching a program through a web browser - a step-by-step guide

I am looking to create a specific sequence of events: A web browser opens, and a user logs in (the exact action is not crucial) The user is then redirected to a new page where another program should automatically open This process is similar to what happ ...

React components do not re-render when the context value changes

The issue with React not re-rendering when the context value changes persists I am using tailwindcss, React(v18.2.0), and vite(3.2.4). I have already attempted i want that clicking on TodoItem should change the completed value in the todo using React con ...

The pagination component in React with Material-ui functions properly on a local environment, but encounters issues when deployed

Looking for some assistance with a persistent issue I've run into. Can anyone lend a hand? [x] The problem persists in the latest release. [x] After checking the repository's issues, I'm confident this is not a duplicate. Current Behavior ...

Is it possible to scroll upwards and then click using Selenium with Python?

I am facing an issue where I need to click on a button using selenium in python. Below is the code snippet I have been using: read_more_buttons = responses[0].find_elements_by_class_name("read-more") if len(read_more_buttons) > 0: ...

Is the popup not opening with just one click?

https://i.stack.imgur.com/09dcf.png Upon clicking the first input cell, the popup opens and closes as expected. However, when closing the initial input and opening another one, an orange mark icon appears but the popup doesn't open until the second c ...

Retrieve the URL of the image from an XML document

Figuring out how to extract the image URL from XML files in Android Studio can be challenging. After analyzing 30 RSS Feed XMLs, I discovered that 95% of them contain ".jpg" images with links starting with "http," not "www." Therefore, I am working on a co ...