原理:将
session_id
和
url
进行记录,下次打开firefox浏览器进行复用
import os,pickle,json,win32api
from selenium import webdriver
from selenium.webdriver import Remote
from selenium.webdriver.chrome import options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.remote.webdriver import WebDriver as remoteWebdriver
from pathlib import Path
import logging as log
log.basicConfig(level=log.INFO)
command_executor_key ="command_executor"
session_id_key ="session_id"classReuseBrowser(Remote):def__init__(self, command_executor, session_id):
self.r_session_id = session_id
Remote.__init__(self, command_executor=command_executor, desired_capabilities={})defstart_session(self, capabilities, browser_profile=None):"""
启用已有session_id
start_session方法重写(去除selenium库Remote类每次实例化都会调用start_session这个方法新建一个会话)
"""ifnotisinstance(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.session_id = self.r_session_id
self.w3c =Falsedefget_firefox_info_path():"""获得firefox信息存储路径"""
temp_path = os.getenv('TEMP')
log.info("temp path:%s"% temp_path)
selenium_path = Path(temp_path).joinpath("selenium")ifnot selenium_path.exists():
selenium_path.mkdir(parents=True, exist_ok=True)returnstr(selenium_path.joinpath("selenium_firefox_info.json").resolve())defclose_firefox():"""
关闭firefox浏览器
@return:
"""
os.system("TASKKILL /F /IM firefox.exe /T")
os.system("TASKKILL /F /IM geckodriver.exe /T")defstart_geckodriver(driver_path):"""打开geckodriver驱动"""
log.info("start driver_path:%s"% driver_path)
win32api.ShellExecute(0,"open",driver_path,None,None,0)classFirefox():def__init__(self,firefox_path, driver_path,command_executor=None,option=None,capabilities=None):
self.firefox_path = firefox_path
self.driver_path = driver_path
ifnot command_executor:
command_executor ="127.0.0.1:4444"
self.command_executor = command_executor
ifnot capabilities:
capabilities = DesiredCapabilities.FIREFOX
self.capabilities = capabilities
ifnot option:
self.option = webdriver.FirefoxOptions()if self.firefox_path:
self.option.binary = firefox_path
#firefox信息存储路径
self.get_firefox_info_path = get_firefox_info_path()defstart_firefox(self):#关闭firefox浏览器和驱动
close_firefox()#重新开启驱动
start_geckodriver(self.driver_path)
driver = remoteWebdriver(command_executor=self.command_executor,
desired_capabilities=self.capabilities,
options=self.option
)#写入
firefox_info ={}
firefox_info[session_id_key]= driver.session_id
firefox_info[command_executor_key]= driver.command_executor._url
log.info("session_id:%s"% driver.session_id)
log.info("url:%s"% driver.command_executor._url)withopen(self.get_firefox_info_path,"w")as write_f:
json.dump(firefox_info,write_f,indent=4,ensure_ascii=False)
self.driver = driver
defresume(self):#获得firefox信息存储路径
info_path = Path(self.get_firefox_info_path)if info_path.exists():# 路径存在# 读取信息withopen(str(info_path),"r")as load_file:try:
load_dict = json.load(load_file)
session_id = load_dict[session_id_key]
command_executor = load_dict[command_executor_key]
log.info("...load info...")
log.info("session_id:%s"% session_id)
log.info("command_executor:%s"% command_executor)
self.driver = ReuseBrowser(command_executor,session_id)
self.driver.refresh()except:
log.info("存储信息有误,重新打开FireFox")
self.start_firefox()else:#路径不存在
self.start_firefox()
调用
if __name__ =="__main__":
firefox_path =r"C:\Program Files\Mozilla Firefox\firefox.exe"
driver_path =r"D:\gitee\selenium_template\cw_rpa\driver\geckodriver.exe"
firefox = Firefox(firefox_path, driver_path)
firefox.resume()
driver = firefox.driver
driver.get("http://www.baidu.com")
本文转载自: https://blog.csdn.net/u012399690/article/details/128332523
版权归原作者 叹一曲当时只道是寻常 所有, 如有侵权,请联系我们删除。
版权归原作者 叹一曲当时只道是寻常 所有, 如有侵权,请联系我们删除。