Python 'NoneType' object has no attribute 'get' on a global variable

1.6k Views Asked by At

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()
2

There are 2 best solutions below

2
Mich On

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)
0
JeffC On

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()