Search code examples
pythonseleniumwebdriver

How can I define driver = webdriver.Firefox without automatically opening the browser?


Here's my code in a simplified way:

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r"F:\geckodriver.exe")

class Acesso():
    def __init__(self):
        driver.get(link)

    def scroll():
        # Function
    driver.click()

    def url():
        # driver.get("https://google.com")

Acesso()

Writing it this way makes the browser to open up as soon as I define my driver variable. Is there any way I can define the webdriver.Firefox() without automatically opening the browser? My goal here is that I want to define the driver variable inside my __init__ function so I can pass an argument to it. Something like this:

def __init__(self, agent):
    self.agent = agent
    driver = webdriver.Firefox(executable_path=r"F:\geckodriver.exe",firefox_profile=agent)

My issue is that writing it this way I can't use the driver variable in other functions and if I define the variable for each function the browser will open automatically at any function's call. Any solutions?


Solution

  • Found a solution setting the driver variable as global. Now I can define inside my function and still use it on other functions.