M
M
micromeow2022-04-15 23:36:37
Python
micromeow, 2022-04-15 23:36:37

Why does the code work with one driver and not with another?

Good day, I recently started learning python

After looking at the multiprocessing guide, I came up with the following code (strictly according to it, as in the guide), it launches several browsers at the same time

import random

from selenium import webdriver
import time
from multiprocessing import Pool

options = webdriver.ChromeOptions()
options.add_argument("user-agent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0")
options.add_argument("--disable-blink-features=AutomationControlled")

def get_data(url):
    try:
        driver = webdriver.Chrome(
            executable_path="chromedriver",
            options=options
        )
        driver.get(url=url)
        time.sleep(5)
    except Exception as ex:
        print(ex)
    finally:
        driver.close()
        driver.quit()


if __name__ == '__main__':
    process_count = int(input("number:  "))
    url = input("url:  ")
    urls_list = [url] * process_count
    print(urls_list)
    p = Pool(processes=process_count)
    p.map(get_data, urls_list)


but I ran into such a problem, for example, I need to run it through driver = undetected_chromedriver.Chrome(), but as soon as I change this line, the code gives UnboundLocalError: local variable 'driver' referenced before assignment, daemonic processes are not allowed to have children

like this the driver cannot be used through such code?

PS in the code with undetected_chromedriver.Chrome() I did import undetected_chromedriver

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2022-04-16
@micromeow

You have the part with the creation of the driver variable wrapped in a try: exception catch, and in finally: there are driver calls, in particular driver.close()
. everything will go along the except branch and then finally:.
And there is a driver call, but it was not created in try due to some error (exception), and the interpreter warns that driver cannot be accessed, since it was not initialized before use.
What to do?
Carefully understand what you are doing inside try:
Then carefully deal with except - there is an Exception interception, i.e. almost any error, but it is necessary to intercept specific classes of errors.
Also, don't call driver until you're sure it's properly initialized before use.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question