Mornon
Mornon

Reputation: 79

Python Selenium: Global driver - 'driver' is not defined in the global scope

the source itself works, but I have the problem that the global driver is undefined, only in VsCode. When I run the source in pycharm, that problem does not exist. Unfortunately, I really do not know how to continue.

The Issue: 'driver' is not defined in the global scope

I used Python 3.7.2 with pytest

from selenium import webdriver
import pytest
from selenium.webdriver.common.keys import Keys


def test_setup():
        global driver
        driver = webdriver.Chrome(executable_path="e:/Webdriver/chromedriver.exe")
        driver.implicitly_wait(10)
        driver.maximize_window()

def test_login():
        driver.get("http://www.dev-crowd.com/wp-login.php")
        driver.find_element_by_id("user_login").send_keys("abc")
        driver.find_element_by_id("user_pass").send_keys("cab")
        driver.find_element_by_id("wp-submit").click()
        x = driver.title("abc")
        assert X == "abc"


def test_teardown():    
        driver.close()
        driver.quit()
        print("Test completed")

Upvotes: 2

Views: 3052

Answers (1)

Jimmy Engelbrecht
Jimmy Engelbrecht

Reputation: 723

The following should work, but i think it should not be necessary:

from selenium import webdriver
import pytest
from selenium.webdriver.common.keys import Keys

driver = None


def test_setup():
    driver = webdriver.Chrome(executable_path="e:/Webdriver/chromedriver.exe")
    driver.implicitly_wait(10)
    driver.maximize_window()


def test_login():
    driver.get("http://www.dev-crowd.com/wp-login.php")
    driver.find_element_by_id("user_login").send_keys("abc")
    driver.find_element_by_id("user_pass").send_keys("cab")
    driver.find_element_by_id("wp-submit").click()
    x = driver.title("abc")
    assert x == "abc"


def test_teardown():
    driver.close()
    driver.quit()
    print("Test completed")

Upvotes: 2

Related Questions