简体   繁体   English

Python Selenium JavaScript 执行器变量

[英]Python Selenium JavaScript Executor Variable

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.我当前的目标是在我的 selenium 浏览器中执行一个脚本,该脚本声明一个 var,然后使用 DevTools 访问控制台日志中的变量。

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.执行后,我尝试访问测试变量,但出现错误。

You have to set the variable to the window scope instead of the function scope您必须将变量设置为 window scope 而不是 function Z31A1FD140BE4BEF2D511E121ECA9

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM