python - 每次无需登录即可打开多个网络驱动程序
问题描述
我正在尝试使用 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)
任何帮助将不胜感激。谢谢!!
解决方案
如上所述,您可以从第一次登录中获取身份验证令牌,然后将其包含在所有后续请求中。
但是,另一种选择(如果您使用基本身份验证)是将用户名和密码添加到 URL 中,例如:
https://username:password@your.domain.com
推荐阅读
- c++ - 我的合并排序代码中出现垃圾值
- sharepoint - 在 PowerApps 中找不到我的表单,但可以从 SharePoint 列表中的链接访问它
- go - golan-gin api 返回无效的 URL 转义 %
- php - 使用 PHP/HTML 在表单验证中突出显示空文本框
- javascript - 无法滚动网格直到记录结束
- bunifu - Bunifu Flat Button: Selected Property Problem (取消选择本身)
- python-3.x - Pip install 在窗口上的 docker 容器中不起作用
- swift - 在 Swift 中将 GPS 元数据写入 EXIF
- javascript - 失去地图功能的上下文
- r - R函数ggplot和lines一起不起作用,每次发送不同的警告消息