设置功能增加browser_path参数和属性;connect_chrome()改为connect_browser()

This commit is contained in:
g1879 2023-01-10 11:56:46 +08:00
parent c72f4ba8d9
commit 568a2a3444
13 changed files with 62 additions and 31 deletions

View File

@ -12,7 +12,7 @@ from requests import Session
from .chromium_base import Timeout, ChromiumBase from .chromium_base import Timeout, ChromiumBase
from .chromium_tab import ChromiumTab from .chromium_tab import ChromiumTab
from .common import connect_chrome from .common import connect_browser
from .config import DriverOptions from .config import DriverOptions
from .chromium_driver import ChromiumDriver from .chromium_driver import ChromiumDriver
@ -47,7 +47,7 @@ class ChromiumPage(ChromiumBase):
if addr_driver_opts is None or isinstance(addr_driver_opts, DriverOptions): if addr_driver_opts is None or isinstance(addr_driver_opts, DriverOptions):
self.options = addr_driver_opts or DriverOptions() # 从ini文件读取 self.options = addr_driver_opts or DriverOptions() # 从ini文件读取
self.address = self.options.debugger_address self.address = self.options.debugger_address
self.process = connect_chrome(self.options)[1] self.process = connect_browser(self.options)[1]
json = self._control_session.get(f'http://{self.address}/json').json() json = self._control_session.get(f'http://{self.address}/json').json()
tab_id = [i['id'] for i in json if i['type'] == 'page'][0] tab_id = [i['id'] for i in json if i['type'] == 'page'][0]
@ -56,7 +56,7 @@ class ChromiumPage(ChromiumBase):
self.address = addr_driver_opts self.address = addr_driver_opts
self.options = DriverOptions(read_file=False) self.options = DriverOptions(read_file=False)
self.options.debugger_address = addr_driver_opts self.options.debugger_address = addr_driver_opts
self.process = connect_chrome(self.options)[1] self.process = connect_browser(self.options)[1]
if not tab_id: if not tab_id:
json = self._control_session.get(f'http://{self.address}/json').json() json = self._control_session.get(f'http://{self.address}/json').json()
tab_id = [i['id'] for i in json if i['type'] == 'page'][0] tab_id = [i['id'] for i in json if i['type'] == 'page'][0]

View File

@ -514,14 +514,14 @@ def _port_is_using(ip: str, port: str) -> Union[bool, None]:
s.close() s.close()
def connect_chrome(option): def connect_browser(option):
"""连接或启动chrome \n """连接或启动浏览器 \n
:param option: DriverOptions对象 :param option: DriverOptions对象
:return: chrome 路径和进程对象组成的元组 :return: chrome 路径和进程对象组成的元组
""" """
system_type = system().lower() system_type = system().lower()
debugger_address = option.debugger_address debugger_address = option.debugger_address
chrome_path = option.chrome_path chrome_path = option.browser_path
debugger_address = debugger_address[7:] if debugger_address.startswith('http://') else debugger_address debugger_address = debugger_address[7:] if debugger_address.startswith('http://') else debugger_address
ip, port = debugger_address.split(':') ip, port = debugger_address.split(':')

View File

@ -53,7 +53,7 @@ def make_absolute_link(link, page: BasePage = None) -> str: ...
def is_js_func(func: str) -> bool: ... def is_js_func(func: str) -> bool: ...
def connect_chrome(option: DriverOptions) -> tuple: ... def connect_browser(option: DriverOptions) -> tuple: ...
def location_in_viewport(page, loc_x: int, loc_y: int) -> bool: ... def location_in_viewport(page, loc_x: int, loc_y: int) -> bool: ...

View File

@ -488,9 +488,6 @@ class DriverOptions(Options):
self.timeouts['pageLoad'] *= 1000 self.timeouts['pageLoad'] *= 1000
self.timeouts['script'] *= 1000 self.timeouts['script'] *= 1000
# if '--no-sandbox' not in self._arguments:
# self._arguments.append('--no-sandbox')
@property @property
def driver_path(self): def driver_path(self):
"""chromedriver文件路径""" """chromedriver文件路径"""
@ -498,6 +495,11 @@ class DriverOptions(Options):
@property @property
def chrome_path(self): def chrome_path(self):
"""浏览器启动文件路径"""
return self.browser_path
@property
def browser_path(self):
"""浏览器启动文件路径""" """浏览器启动文件路径"""
return self.binary_location or 'chrome' return self.binary_location or 'chrome'
@ -718,11 +720,12 @@ class DriverOptions(Options):
self.page_load_strategy = value.lower() self.page_load_strategy = value.lower()
return self return self
def set_paths(self, driver_path=None, chrome_path=None, local_port=None, debugger_address=None, download_path=None, def set_paths(self, driver_path=None, chrome_path=None, browser_path=None, local_port=None,
user_data_path=None, cache_path=None): debugger_address=None, download_path=None, user_data_path=None, cache_path=None):
"""快捷的路径设置函数 \n """快捷的路径设置函数 \n
:param driver_path: chromedriver.exe路径 :param driver_path: chromedriver.exe路径
:param chrome_path: chrome.exe路径 :param chrome_path: chrome.exe路径
:param browser_path: 浏览器可执行文件路径
:param local_port: 本地端口号 :param local_port: 本地端口号
:param debugger_address: 调试浏览器地址127.0.0.1:9222 :param debugger_address: 调试浏览器地址127.0.0.1:9222
:param download_path: 下载文件路径 :param download_path: 下载文件路径
@ -736,6 +739,9 @@ class DriverOptions(Options):
if chrome_path is not None: if chrome_path is not None:
self.binary_location = str(chrome_path) self.binary_location = str(chrome_path)
if browser_path is not None:
self.binary_location = str(browser_path)
if local_port is not None: if local_port is not None:
self.debugger_address = '' if local_port == '' else f'127.0.0.1:{local_port}' self.debugger_address = '' if local_port == '' else f'127.0.0.1:{local_port}'

View File

@ -5,6 +5,7 @@
""" """
from configparser import RawConfigParser from configparser import RawConfigParser
from http.cookiejar import Cookie from http.cookiejar import Cookie
from pathlib import Path
from typing import Any, Union, List from typing import Any, Union, List
from requests.cookies import RequestsCookieJar from requests.cookies import RequestsCookieJar
@ -158,6 +159,9 @@ class DriverOptions(Options):
@property @property
def chrome_path(self) -> str: ... def chrome_path(self) -> str: ...
@property
def browser_path(self) -> str: ...
@property @property
def user_data_path(self) -> str: ... def user_data_path(self) -> str: ...
@ -203,8 +207,9 @@ class DriverOptions(Options):
def set_page_load_strategy(self, value: str) -> 'DriverOptions': ... def set_page_load_strategy(self, value: str) -> 'DriverOptions': ...
def set_paths(self, def set_paths(self,
driver_path: str = None, driver_path: Union[str, Path] = None,
chrome_path: str = None, chrome_path: Union[str, Path] = None,
browser_path: Union[str, Path] = None,
local_port: Union[int, str] = None, local_port: Union[int, str] = None,
debugger_address: str = None, debugger_address: str = None,
download_path: str = None, download_path: str = None,

View File

@ -1,10 +1,10 @@
[paths] [paths]
chromedriver_path = D:\coding\projects\DrissionPage\DrissionPage\chromedriver.exe chromedriver_path =
tmp_path = tmp_path =
[chrome_options] [chrome_options]
debugger_address = 127.0.0.1:9222 debugger_address = 127.0.0.1:9222
binary_location = C:\Program Files\Google\Chrome\Application\chrome.exe binary_location = chrome
arguments = ['--no-first-run', '--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--disable-infobars', '--disable-popup-blocking'] arguments = ['--no-first-run', '--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--disable-infobars', '--disable-popup-blocking']
extensions = [] extensions = []
experimental_options = {'prefs': {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}, 'plugins.plugins_list': [{'enabled': False, 'name': 'Chrome PDF Viewer'}]}, 'useAutomationExtension': False, 'excludeSwitches': ['enable-automation']} experimental_options = {'prefs': {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}, 'plugins.plugins_list': [{'enabled': False, 'name': 'Chrome PDF Viewer'}]}, 'useAutomationExtension': False, 'excludeSwitches': ['enable-automation']}

View File

@ -14,7 +14,7 @@ from selenium.webdriver.chrome.options import Options
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from tldextract import extract from tldextract import extract
from .common import get_pid_from_port, connect_chrome from .common import get_pid_from_port, connect_browser
from .config import SessionOptions, DriverOptions, cookies_to_tuple, session_options_to_dict from .config import SessionOptions, DriverOptions, cookies_to_tuple, session_options_to_dict
@ -103,7 +103,7 @@ class Drission(object):
# -----------若指定debug端口且该端口未在使用中则先启动浏览器进程----------- # -----------若指定debug端口且该端口未在使用中则先启动浏览器进程-----------
if self.driver_options.debugger_address: if self.driver_options.debugger_address:
# 启动浏览器进程,同时返回该进程使用的 chrome.exe 路径 # 启动浏览器进程,同时返回该进程使用的 chrome.exe 路径
chrome_path, self._debugger = connect_chrome(self.driver_options) chrome_path, self._debugger = connect_browser(self.driver_options)
# -----------创建WebDriver对象----------- # -----------创建WebDriver对象-----------
self._driver = create_driver(chrome_path, driver_path, self.driver_options) self._driver = create_driver(chrome_path, driver_path, self.driver_options)

View File

@ -30,6 +30,7 @@ def show_settings(ini_path=None):
def set_paths(driver_path=None, def set_paths(driver_path=None,
chrome_path=None, chrome_path=None,
browser_path=None,
local_port=None, local_port=None,
debugger_address=None, debugger_address=None,
tmp_path=None, tmp_path=None,
@ -41,6 +42,7 @@ def set_paths(driver_path=None,
"""快捷的路径设置函数 \n """快捷的路径设置函数 \n
:param driver_path: chromedriver.exe路径 :param driver_path: chromedriver.exe路径
:param chrome_path: chrome.exe路径 :param chrome_path: chrome.exe路径
:param browser_path: 浏览器可执行文件路径
:param local_port: 本地端口号 :param local_port: 本地端口号
:param debugger_address: 调试浏览器地址127.0.0.1:9222 :param debugger_address: 调试浏览器地址127.0.0.1:9222
:param download_path: 下载文件路径 :param download_path: 下载文件路径
@ -62,6 +64,9 @@ def set_paths(driver_path=None,
if chrome_path is not None: if chrome_path is not None:
om.set_item('chrome_options', 'binary_location', format_path(chrome_path)) om.set_item('chrome_options', 'binary_location', format_path(chrome_path))
if browser_path is not None:
om.set_item('chrome_options', 'binary_location', format_path(browser_path))
if local_port is not None: if local_port is not None:
om.set_item('chrome_options', 'debugger_address', format_path(f'127.0.0.1:{local_port}')) om.set_item('chrome_options', 'debugger_address', format_path(f'127.0.0.1:{local_port}'))

View File

@ -11,6 +11,7 @@ def show_settings(ini_path: str = None) -> None: ...
def set_paths(driver_path: str = None, def set_paths(driver_path: str = None,
chrome_path: str = None, chrome_path: str = None,
browser_path: str = None,
local_port: Union[int, str] = None, local_port: Union[int, str] = None,
debugger_address: str = None, debugger_address: str = None,
tmp_path: str = None, tmp_path: str = None,

View File

@ -1,7 +1,7 @@
* [📣 1 概述](README.md) * [📣 1 概述](README.md)
* [☀️ 2 特性和亮点](#) * [☀️ 2 特性和亮点](#)
* [💖 2.1 贴心设计](特性和亮点\贴心设计.md) * [💖 2.1 贴心设计](特性和亮点\贴心设计.md)
* [🌟 2.2 特性演示](#) * [🌟 2.2 特性演示](#)
* [⭐ 与 requests 对比](特性和亮点\特性演示\与requests代码对比.md) * [⭐ 与 requests 对比](特性和亮点\特性演示\与requests代码对比.md)
@ -11,17 +11,17 @@
* [⭐ 下载文件](特性和亮点\特性演示\下载文件.md) * [⭐ 下载文件](特性和亮点\特性演示\下载文件.md)
* [🧭 3 入门指南](#) * [🧭 3 入门指南](#)
* [🌏 3.1 基本概念](入门指南\基本概念.md) * [🌏 3.1 安装和导入](入门指南\安装和导入.md)
* [🌏 3.2 安装和导入](入门指南\安装和导入.md) * [🌏 3.2 准备工作](入门指南\准备工作.md)
* [🌏 3.3 准备工作](入门指南\准备工作.md) * [🌏 3.3 上手示例](#)
* [🌏 3.4 上手示例](#)
* [🌐 控制浏览器](入门指南\上手示例\控制浏览器.md) * [🌐 控制浏览器](入门指南\上手示例\控制浏览器.md)
* [🌐 收发数据包](入门指南\上手示例\收发数据包.md) * [🌐 收发数据包](入门指南\上手示例\收发数据包.md)
* [🌐 模式切换](入门指南\上手示例\模式切换.md) * [🌐 模式切换](入门指南\上手示例\模式切换.md)
* [🌏 3.4 基本概念](入门指南\基本概念.md)
* [🛠 4 使用方法](#) * [🛠 4 使用方法](#)
* [🔨 4.0 简介](WebPage使用方法\3.0简介.md) * [🔨 4.0 简介](WebPage使用方法\3.0简介.md)
* [🔨 4.1 创建页面对象](WebPage使用方法\3.1创建页面对象.md) * [🔨 4.1 创建页面对象](WebPage使用方法\3.1创建页面对象.md)
* [🔨 4.2 访问网页](WebPage使用方法\3.2访问网页.md) * [🔨 4.2 访问网页](WebPage使用方法\3.2访问网页.md)
@ -35,20 +35,20 @@
* [🔨 4.10 动作链](WebPage使用方法\3.10动作链.md) * [🔨 4.10 动作链](WebPage使用方法\3.10动作链.md)
* [📝 5 启动配置](#) * [📝 5 启动配置](#)
* [🗒️ 5.1 概述](启动配置\概述.md) * [🗒️ 5.1 概述](启动配置\概述.md)
* [🗒️ 5.2 浏览器启动配置](启动配置\浏览器启动配置.md) * [🗒️ 5.2 浏览器启动配置](启动配置\浏览器启动配置.md)
* [🗒️ 5.3 Session 启动配置](启动配置\Session启动配置.md) * [🗒️ 5.3 Session 启动配置](启动配置\Session启动配置.md)
* [🗒️ 5.4 使用配置文件](启动配置\使用配置文件.md) * [🗒️ 5.4 使用配置文件](启动配置\使用配置文件.md)
* [🧰 6 进阶使用](#) * [🧰 6 进阶使用](#)
* [⚙️ 6.1 打包程序](进阶使用\打包程序.md) * [⚙️ 6.1 打包程序](进阶使用\打包程序.md)
* [⚙️ 6.2 监听浏览器网络](进阶使用\监听浏览器网络.md) * [⚙️ 6.2 监听浏览器网络](进阶使用\监听浏览器网络.md)
* [⚙️ 6.3 下载文件](进阶使用\下载文件.md) * [⚙️ 6.3 下载文件](进阶使用\下载文件.md)
* [🛠 7 旧版使用方法](#) * [🛠 7 旧版使用方法](#)
* [🔨 7.0 简介](MixPage使用方法\简介.md) * [🔨 7.0 简介](MixPage使用方法\简介.md)
* [🔨 7.1 创建页面对象](MixPage使用方法\创建页面对象.md) * [🔨 7.1 创建页面对象](MixPage使用方法\创建页面对象.md)
* [🔨 7.2 访问网页](MixPage使用方法\访问网页.md) * [🔨 7.2 访问网页](MixPage使用方法\访问网页.md)
@ -64,7 +64,7 @@
* [🔨 7.12 DriverPage 和 SessionPage](MixPage使用方法\DriverPage和SessionPage.md) * [🔨 7.12 DriverPage 和 SessionPage](MixPage使用方法\DriverPage和SessionPage.md)
* [⚡️ 8 示例和技巧](#) * [⚡️ 8 示例和技巧](#)
* [🌠 自动登录码云](示例和技巧\自动登录码云.md) * [🌠 自动登录码云](示例和技巧\自动登录码云.md)
* [🌠 采集猫眼电影TOP100榜](示例和技巧\采集猫眼电影TOP100榜.md) * [🌠 采集猫眼电影TOP100榜](示例和技巧\采集猫眼电影TOP100榜.md)
* [🌠 下载星巴克产品图片](示例和技巧\下载星巴克产品图片.md) * [🌠 下载星巴克产品图片](示例和技巧\下载星巴克产品图片.md)

View File

@ -1,6 +1,6 @@
本节讲解 DrissionPage 的一些基本概念。了解它大概的构成。 本节讲解 DrissionPage 的一些基本概念。了解它大概的构成。
如果您觉得有点懵,可直接跳过本节,从下一节直接上手代码 如果您觉得有点懵,可直接跳过本节。
# ✔️ 网页自动化 # ✔️ 网页自动化

View File

@ -1,3 +1,17 @@
# ✔️运行环境
## 📍 操作系统
Windows 或 Linux。
作者没有 Mac没测试过估计是可以的。
## 📍 python 版本
支持 python 3.6 及以上版本。
# ✔️ 安装 # ✔️ 安装
请使用 pip 安装 DrissionPage 请使用 pip 安装 DrissionPage

View File

@ -22,7 +22,7 @@ tmp_path =
debugger_address = 127.0.0.1:9222 debugger_address = 127.0.0.1:9222
; 浏览器可执行文件路径 ; 浏览器可执行文件路径
binary_location = binary_location = chrome
; 配置信息 ; 配置信息
arguments = [ arguments = [