Selenium Web 驱动程序可以访问 javascript 全局变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10455130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:56:23  来源:igfitidea点击:

Can Selenium web driver have access to javascript global variables?

javascriptpythondjangoselenium

提问by dave

Hi: I'm writing tests for django with javascript and I was wondering if the Selenium webdriver can access a javascript global variable. mypagehas a script that has a global variable I'd like to access. Is it possible? Thanks!

嗨:我正在用 javascript 为 django 编写测试,我想知道 Selenium webdriver 是否可以访问 javascript 全局变量。mypage有一个脚本,其中有一个我想访问的全局变量。是否可以?谢谢!

from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver

class TestEditorSelenium(LiveServerTestCase):
    def setUp(self):
        self.driver = WebDriver()

    def test_mytest(self):
        self.driver.get('%s%s' % (self.live_server_url, '/mypage/'))

回答by Scott

Yes, you should be able to that with code similar to the below:

是的,您应该能够使用类似于以下的代码:

browser.execute_script("return globalVar;")