【问题标题】:Open 2 Webdrivers at same time with multi-threading使用多线程同时打开 2 个 Webdriver
【发布时间】:2023-05-03 19:16:01
【问题描述】:

我正在使用 SeleniumWebdriver 进行 Crawl 项目。由于我需要在大时抓取该数据,因此我想将其拆分为 2 threads 并同时运行。但是当我同时启动2个Webdrivers时,我的代码无法识别哪个driver属于哪个thread来填写信息。

  1. 这是我设置的代码 2 threads 运行 main 函数:
if __name__ == '__main__':
    data = load_data(INPUT_DIR)

    t1_data = data[:250]
    t2_data = data[250:]

    try:
        _thread.start_new_thread(main, (t1_data, LOG_FILE_T1))
        _thread.start_new_thread(main, (t2_data, LOG_FILE_T2))
    except:
        print ("Error: unable to start thread")

    while 1:
        pass 
  1. 这段代码我启动Webdriver
def start_driver():

    global driver

    options = Options()
    options.add_argument("--disable-notification")
    options.add_argument("--disable-infobars")
    options.add_argument("--mute-audio")
    #options.add_argument("headless")

    driver = webdriver.Chrome(options=options)
  1. 两个Webdrivers启动后填写用户名/密码 facebook.com 上的信息
def login(email, password):
    """ Logging into our own profile """

    try:
        driver.get('https://mbasic.facebook.com')
        time.sleep(DELAY_TIME)

        driver.find_element_by_name('email').send_keys(email)
        driver.find_element_by_name('pass').send_keys(password)
        driver.find_element_by_name('login').click()

        # deal with "Not Now" button if it show off at first time
        not_now_button = driver.find_element_by_xpath("//a")
        if not_now_button.size != 0:
            not_now_button.click()

    except Exception as e:
        print('Error in Login')
        print(e)
        exit()

send_keys这一步,两个threads在同一个文本框中填写1Webdriver。 如何更改我的代码 2 threads 可以看到不同的 Webdrive 并填写信息?

【问题讨论】:

    标签: python multithreading selenium selenium-webdriver web-crawler


    【解决方案1】:

    刚刚找到解决方案,如果有人需要,我想在这里分享。

    我将更改为本地driver,而不是global driver,并将其传递给每个function

    def start_driver():
    
        options = Options()
        options.add_argument("--disable-notification")
        options.add_argument("--disable-infobars")
        options.add_argument("--mute-audio")
        #options.add_argument("headless")
    
        driver = webdriver.Chrome(options=options)
        return driver
    
    def login(driver, email, password):
        """ Logging into our own profile """
    
        try:
            driver.get('https://mbasic.facebook.com')
            time.sleep(DELAY_TIME)
    
            driver.find_element_by_name('email').send_keys(email)
            driver.find_element_by_name('pass').send_keys(password)
            driver.find_element_by_name('login').click()
    
            # deal with "Not Now" button if it show off at first time
            not_now_button = driver.find_element_by_xpath("//a")
            if not_now_button.size != 0:
                not_now_button.click()
    
        except Exception as e:
            print('Error in Login')
            print(e)
            exit()
    

    由此,2个threads可以看到不同的Webdriver并填写自己的信息。

    【讨论】:

      最近更新 更多