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.
-
@ArundeepChohan And how would I do that?dracoDevs– dracoDevs2021年03月07日 04:34:52 +00:00Commented Mar 7, 2021 at 4:34
1 Answer 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
default