0

I have a script that uses a global object (from selenium, driver=Webdriver.Firefox()) repeatedly. But at some point when the script is waiting too long for page to load (in the middle of an operation) the script is then rebooting, relaunching the script and continuing from the same spot, as in:

def do_web_stuff():
    "script stuff going on"
    if "page takes too long":
        reboot "closes firefox browser", "run script from when stopped"
        "continue"
if __name__ == "__main__":
    driver = webdriver.Firefox()

My problem is when i reboot, driver in no longer active there for if i reboot the script I get error 111 "Connection refused" which is understandable. If i relaunch driver in the reboot section i get NameError: global name 'driver' is not defined.

I've thought about making driver global in the reboot function but then i get a SyntaxError: "is local and global". So, hmmm, what to do? I would really prefer the object to be in the global scope instead of passing it from one function to the other.

What would be an good practice in this situation?

1 Answer 1

2

If you set a variable in Python function it is implicit local.

What you need to do is use global keyword to tell that you want to set module-global variable.

 driver = None

 def do_stuff():
      global driver
      if reboot:
           driver = webdriver.Firefox()

 if __name__ == "__main__":
      driver = webdriver.Firefox()
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Mikko, it's clearer to just set a variable and then make it global, instead of having a global var and reincarnate it from the inside.

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.