selenium连接已打开的Firefox浏览器

原理:将session_idurl进行记录,下次打开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"


class ReuseBrowser(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):
        """
        启用已有session_id
        start_session方法重写(去除selenium库Remote类每次实例化都会调用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.session_id = self.r_session_id
        self.w3c = False

def get_firefox_info_path():
    """获得firefox信息存储路径"""
    temp_path = os.getenv('TEMP')
    log.info("temp path:%s" % temp_path)
    selenium_path = Path(temp_path).joinpath("selenium")
    if not selenium_path.exists():
        selenium_path.mkdir(parents=True, exist_ok=True)
    return str(selenium_path.joinpath("selenium_firefox_info.json").resolve())

def close_firefox():
    """
    关闭firefox浏览器
    @return:
    """
    os.system("TASKKILL /F /IM firefox.exe /T")
    os.system("TASKKILL /F /IM geckodriver.exe /T")

def start_geckodriver(driver_path):
    """打开geckodriver驱动"""
    log.info("start driver_path:%s" % driver_path)
    win32api.ShellExecute(0,"open",driver_path,None,None,0)

class Firefox():
    def __init__(self,firefox_path, driver_path,command_executor=None,option=None,capabilities=None):
        self.firefox_path = firefox_path
        self.driver_path = driver_path
        if not command_executor:
            command_executor = "127.0.0.1:4444"
        self.command_executor = command_executor
        if not capabilities:
            capabilities = DesiredCapabilities.FIREFOX
        self.capabilities = capabilities
        if not option:
            self.option = webdriver.FirefoxOptions()
        if self.firefox_path:
            self.option.binary = firefox_path
        #firefox信息存储路径
        self.get_firefox_info_path = get_firefox_info_path()
    
    def start_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)
        with open(self.get_firefox_info_path,"w") as write_f:
            json.dump(firefox_info,write_f,indent=4,ensure_ascii=False)
        self.driver = driver

    def resume(self):
        #获得firefox信息存储路径
        info_path = Path(self.get_firefox_info_path)
        if info_path.exists():
            # 路径存在
            # 读取信息
            with open(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")

你可能感兴趣的:(python,selenium,python,firefox,自动化)