2

My current goal is to execute a script to my selenium browser that declares a var and then using DevTools access the variable in console log.

Here is the conflicting script:

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"
)

Upon execution I try to access the test variable and I get an error.

1
  • @ArundeepChohan And how would I do that? Commented Mar 7, 2021 at 4:34

1 Answer 1

1

You have to set the variable to the window scope instead of the function scope

driver.execute_script("this.test = 'Test Now'")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.