0

I don't understand why I cannot set a global variable driver in Selenium

I get this error in the Load() function on driver

Exception has occurred: AttributeError
'NoneType' object has no attribute 'get'
  File "D:\Code\edge_script.py", line xx, in load
    driver.get("https://www.google.com")
    ^^^^^^^^^^
  File "D:\Code\edge_script.py", line xx, in main
    load()
  File "D:\Code\edge_script.py", line xx, in <module>
    main()
AttributeError: 'NoneType' object has no attribute 'get'

code is below

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from pathlib import Path
import time
import subprocess
import re

from msedge.selenium_tools import Edge, EdgeOptions

#global variables
driver = None
    
def init():
    subprocess.run("taskkill /f /im msedge.exe")

    edge_options = EdgeOptions()
    edge_options.use_chromium = True    

    #Here you set the path of the profile ending with User Data not the profile folder
    path  = "user-data-dir="+str(Path.home())+"\\AppData\\Local\\Microsoft\\Edge\\User Data"
    print(path)
    edge_options.add_argument(path); 

    #Here you specify the actual profile folder    
    edge_options.add_argument("--profile-directory=Default")
    edge_options.add_argument("--no-sandbox") 
    edge_options.add_argument("--disable-setuid-sandbox") 
    edge_options.add_argument("--remote-debugging-port=9222") 
    edge_options.add_argument("--disable-dev-shm-using") 
    edge_options.add_argument("--disable-extensions") 
    edge_options.add_argument("--disable-gpu") 

    edge_options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
    driver = Edge(options = edge_options, executable_path = "D:\\msedgedriver.exe")

def load():
    # navigate to the website
    driver.get("https://www.google.com") #<<<ERROR HERE

    # wait for the page to load
    time.sleep(5)

def close():
    # close the driver
    driver.quit()

def main():

    init()
    load()
    close()

if __name__ == "__main__":
    main()
Mich
  • 3,188
  • 4
  • 37
  • 85
  • If ```driver``` is a global variable, aren't you supposed to include the ```global driver``` statement in your ```load()``` function? – ewokx Mar 31 '23 at 04:12
  • 1
    Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Ture Pålsson Mar 31 '23 at 04:15

2 Answers2

0

The reason you are getting this error is because you have defined driver as a global variable and it's set to None.

In init(), you assigning an instance of the Edge driver to driver but since you haven't declared driver as global, python is just assigning it to a local variable named driver which is destroyed as soon as you leave init().

Add a global declaration inside init() to fix this issue and you don't need it in load() or elsewhere because you're only *reading* driver not assigning to it.

Your final code should be

#global variables
driver = None
    
def init():
    global driver # <<<<<< add this here
    subprocess.run("taskkill /f /im msedge.exe")

    edge_options = EdgeOptions()
    edge_options.use_chromium = True    

    #Here you set the path of the profile ending with User Data not the profile folder
    path  = "user-data-dir="+str(Path.home())+"\\AppData\\Local\\Microsoft\\Edge\\User Data"
    print(path)
    edge_options.add_argument(path); 

    #Here you specify the actual profile folder    
    edge_options.add_argument("--profile-directory=Default")
    edge_options.add_argument("--no-sandbox") 
    edge_options.add_argument("--disable-setuid-sandbox") 
    edge_options.add_argument("--remote-debugging-port=9222") 
    edge_options.add_argument("--disable-dev-shm-using") 
    edge_options.add_argument("--disable-extensions") 
    edge_options.add_argument("--disable-gpu") 

    edge_options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
    driver = Edge(options = edge_options, executable_path = "D:\\msedgedriver.exe")

def load():
    # navigate to the website
    driver.get("https://www.google.com") #<<<ERROR HERE

    # wait for the page to load
    time.sleep(5)

def close():
    # close the driver
    driver.quit()

def main():

    init()
    load()
    close()

if __name__ == "__main__":
    main()
JeffC
  • 22,180
  • 5
  • 32
  • 55
-3

Need to declare global variable in python function unlike other languages

def load():
    global driver
    # navigate to the website
    driver.get("https://www.google.com") 

    # wait for the page to load
    time.sleep(5)
Mich
  • 3,188
  • 4
  • 37
  • 85
  • ditto within the close() function – ewokx Mar 31 '23 at 04:14
  • 3
    **no**, this is all wrong. @Mich this is not the use case for `global`. You only need it when *setting* the variable, not reading it. Thus, adding this under `init()` function instead, should fix the issue. – rv.kvetch Mar 31 '23 at 04:15