python重新打开上次打开的webdriver窗口,不用再次输入密码
from selenium.webdriver import Remote from selenium.webdriver.chrome import options from selenium.common.exceptions import InvalidArgumentException import sys import configparser class ReuseChrome(Remote): def __init__(self, command_executor, session_id): self.r_session_id = session_id Remote.__init__(self, command_executor=command_executor, desired_capabilities={}) def start_session(self, capabilities, browser_profile=None): """ 重写start_session方法 """ if not isinstance(capabilities, dict): raise InvalidArgumentException("Capabilities must be a dictionary") if browser_profile: if "moz:firefoxOptions" in capabilities: capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded else: capabilities.update({'firefox_profile': browser_profile.encoded}) #self.capabilities = options.Options().to_capabilities() #self.capabilities = options.to_capabilities() self.session_id = self.r_session_id self.w3c = False cf = configparser.ConfigParser() ac=cf.read("my.ini") driver=webdriver.Chrome() driver.implicitly_wait(10) # 隐式等待 driver.get("https://www.baidu.com") #保存当前的记录信息 executor_url = driver.command_executor._url session_id = driver.session_id cf.add_section("ssh") cf.set("ssh","executor_url",executor_url) cf.set("ssh","session_id",session_id) cf.write(open("my.ini","w")) #第二次运行时读取executor_url,session_id executor_url = cf.get("ssh","executor_url") session_id = cf.get("ssh","session_id") try: del driver except: pass driver = ReuseChrome(command_executor=executor_url, session_id=session_id) driver.get(url)