首页 > 解决方案 > 每次无需登录即可打开多个网络驱动程序

问题描述

我正在尝试使用 ThreadsPoolExecutor 运行 selenium。该网站需要登录,我正在尝试加快我在网站上尝试执行的操作。但是每次一个线程打开chrome时,我都需要重新登录,它有时只是挂起。我先登录一次,而不使用线程进行一些处理。从这里开始,我喜欢打开一些 chome 网络驱动程序而无需重新登录。有没有解决的办法?PS:网站的 url 中没有 id 和 password 字符串。

def startup(dirPath):
    # Start the WebDriver, load options
    options = webdriver.ChromeOptions()
    options.add_argument("--disable-infobars")
    options.add_argument("--enable-file-cookies")
    params = {'behavior': 'allow', 'downloadPath': dirPath}

    wd = webdriver.Chrome(options=options, executable_path=r"C:\Chrome\chromedriver.exe")
    wd.execute_cdp_cmd('Page.setDownloadBehavior', params)
    # wd.delete_all_cookies()
    wd.set_page_load_timeout(30)
    wd.implicitly_wait(10)

return wd    


def webLogin(dID, pw, wd):
    wd.get('some url')

    # Login, clear any outstanding login in id
    wd.find_element_by_id('username').clear()
    wd.find_element_by_id('username').send_keys(dID)
    wd.find_element_by_id('password').clear()
    wd.find_element_by_id('password').send_keys(pw)
    wd.find_element_by_css_selector('.button').click()


if __name__ == '__main__':

    dirPath, styleList = firstProcessing()
    loginAndClearLB(dID, dPw, dirPath) # calls startup & webLogin, this is also my 1st login
    # many webdrivers spawned here
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        results = {executor.submit(addArtsToLB, dID, dPw, dirPath, style): style for style in styleList} 

    #Do other stuff
    wd2 = startup(dirPath)
    webLogin(dID, dPw, wd2)
    startDL(wd2)
    logOut(wd2, dirPath)

任何帮助将不胜感激。谢谢!!

标签: pythonselenium

解决方案


如上所述,您可以从第一次登录中获取身份验证令牌,然后将其包含在所有后续请求中。

但是,另一种选择(如果您使用基本身份验证)是将用户名和密码添加到 URL 中,例如:

https://username:password@your.domain.com

推荐阅读