Home > database >  Python - Selenium - Single thread to multiple threads
Python - Selenium - Single thread to multiple threads

Time:02-01

I have an automation project made with Python and Selenium which I'm trying to make it run with multiple browsers in parallel.

The current workflow:

  • open a browser for manual login
  • save cookies for later use
  • in a loop, open additional browsers, load the saved session in each newly opened browser

The described workflow is opening some browsers, one by one, until all required browsers are opened.

My code contains several classes: Browser and Ui.

The object instantiated with Ui class contains a method which at some point executes the following code:

    for asset in Inventory.assets:
        self.browsers[asset] = ui.Browser()
        # self.__open_window(asset)            # if it is uncommented, the code is working properly without multi threading part; all the browsers are opened one by one

    # try 1
    # threads = []
    # for asset in Inventory.assets:
    #     threads.append(Thread(target=self.__open_window, args=(asset,), name=asset))
    # for thread in threads:
    #     thread.start()

    # try 2
    # with concurrent.futures.ThreadPoolExecutor() as executor:
    #     futures = []
    #     for asset in Inventory.assets:
    #         futures.append(executor.submit(self.__open_window, asset=asset))
    #     for future in concurrent.futures.as_completed(futures):
    #         print(future.result())

The problem appear when self.__open_window is executed within a thread. There i get an error related to Selenium, something like: 'NoneType' object has no attribute 'get', when self.driver.get(url) is called from the Browser class.

def __open_window(self, asset):
    self.interface = self.browsers[asset]
    self.interface.open_browser()

In class Browser:

def open_browser(self, driver_path=""):
    # ...
    options = webdriver.ChromeOptions()
    # ...
    #
    web_driver = webdriver.Chrome(executable_path=driver_path, options=options)
    #
    self.driver = web_driver
    self.opened_tabs["default"] = web_driver.current_window_handle
    #
    # ...

def get_url(self, url):
    try:
        self.driver.get(url)      # this line cause problems ...
    except Exception as e:
        print(e)

My questions are: Why do i have this issue in a multi threading environment? What should i do in order to make the code work properly?

Thank You

CodePudding user response:

I found the mistake, it was because of a wrong object reference. After modification the code is working well.

I updated the following lines at __open_window:

    def __open_window(self, asset, browser):
        browser.interface = self.browsers[asset]
        browser.interface.open_browser()

and in # try 1 code section:

threads.append(Thread(target=self.__open_window, args=(asset, browser, ), name=asset))
  •  Tags:  
  • Related