5

I am using Python Selenium to open a Firefox browser and go to a URL. The function I am using to do this is...

def openurl_function():
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.keys import Keys

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get('http://www.example.com')

When I run the function it always opens a new instance of FireFox, is there a way to have it to just open using the same browser instance?

Currently if I run the function 10 times then I get 10 FireFox browsers open.

1 Answer 1

12

Just keep reusing the same driver. You are creating a new browser, every time you call

driver = webdriver.Firefox()

Also, because you never quit() on your driver, you will probably have all the browsers stay open as orphans because you deleted the handle to them when you created a new browser.

Sign up to request clarification or add additional context in comments.

2 Comments

So would I need to move the driver = webdriver.Firefox() outside of the function so it only runs the once?
Yes. Just create the object in global context and pass it to your function every time (or use an object property if it's within a class).

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.