13

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

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/'))

1 Answer 1

27

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

browser.execute_script("return globalVar;")
Sign up to request clarification or add additional context in comments.

2 Comments

print self.driver.execute_script("return globalVar;") worked...thanks!
Interesting.. I can get an expected value execute_script("return window.name;") or execute_script("return window.location;") but not for execute_script("return window;") The last one returns an empty array for me....

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.