继续改进WebPage设置,未完成;完善设置类

This commit is contained in:
g1879 2023-01-18 23:06:43 +08:00
parent 6cfb7dc322
commit 62b83b0749
13 changed files with 62 additions and 63 deletions

View File

@ -38,12 +38,12 @@ class ChromiumDriver(object):
:param address: 浏览器连接地址 :param address: 浏览器连接地址
""" """
self.id = tab_id self.id = tab_id
self.address = address
self.type = tab_type self.type = tab_type
self.debug = False self.debug = False
self.has_alert = False self.has_alert = False
self._websocket_url = f'ws://{address}/devtools/{tab_type}/{tab_id}' self._websocket_url = f'ws://{address}/devtools/{tab_type}/{tab_id}'
self._address = address
self._cur_id = 0 self._cur_id = 0
self._ws = None self._ws = None
@ -60,16 +60,6 @@ class ChromiumDriver(object):
self.method_results = {} self.method_results = {}
self.event_queue = Queue() self.event_queue = Queue()
@property
def websocket_url(self):
"""返回websocket连接地址"""
return self._websocket_url
@property
def address(self):
"""返回连接地址"""
return self._address
def _send(self, message, timeout=None): def _send(self, message, timeout=None):
"""发送信息到浏览器,并返回浏览器返回的信息 """发送信息到浏览器,并返回浏览器返回的信息
:param message: 发送给浏览器的数据 :param message: 发送给浏览器的数据

View File

@ -17,6 +17,7 @@ class ChromiumDriver(object):
_STARTED_: str _STARTED_: str
_STOPPED_: str _STOPPED_: str
id: str id: str
address: str
type: str type: str
debug: bool debug: bool
has_alert: bool has_alert: bool
@ -34,12 +35,6 @@ class ChromiumDriver(object):
def __init__(self, tab_id: str, tab_type: str, address: str): ... def __init__(self, tab_id: str, tab_type: str, address: str): ...
@property
def websocket_url(self) -> str: ...
@property
def address(self) -> str: ...
def _send(self, message: dict, timeout: Union[int, float] = None) -> dict: ... def _send(self, message: dict, timeout: Union[int, float] = None) -> dict: ...
def _recv_loop(self) -> None: ... def _recv_loop(self) -> None: ...

View File

@ -64,20 +64,24 @@ class ChromiumPage(ChromiumBase):
self._init_page(tab_id) self._init_page(tab_id)
self._set_options() self._set_options()
self._set_chromium_options()
self._get_document() self._get_document()
self._first_run = False self._first_run = False
def _set_options(self): def _set_options(self):
"""从配置中读取设置""" """设置WebPage中与s模式共用的配置便于WebPage覆盖掉"""
self._timeouts = Timeout(self, self._timeouts = Timeout(self,
page_load=self._driver_options.timeouts['pageLoad'], page_load=self._driver_options.timeouts['pageLoad'],
script=self._driver_options.timeouts['script'], script=self._driver_options.timeouts['script'],
implicit=self._driver_options.timeouts['implicit']) implicit=self._driver_options.timeouts['implicit'])
self._page_load_strategy = self._driver_options.page_load_strategy self._page_load_strategy = self._driver_options.page_load_strategy
self.download_set.save_path(self._download_path)
def _set_chromium_options(self):
"""设置浏览器专有的配置"""
self._main_tab = self.tab_id self._main_tab = self.tab_id
self._alert = Alert() self._alert = Alert()
self._window_setter = None self._window_setter = None
self._download_path = self._driver_options.download_path
def _init_page(self, tab_id=None): def _init_page(self, tab_id=None):
"""新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化

View File

@ -34,6 +34,8 @@ class ChromiumPage(ChromiumBase):
def _set_options(self) -> None: ... def _set_options(self) -> None: ...
def _set_chromium_options(self) -> None: ...
@property @property
def tabs_count(self) -> int: ... def tabs_count(self) -> int: ...

View File

@ -124,11 +124,9 @@ class ChromiumOptions(object):
:return: 当前对象 :return: 当前对象
""" """
self.remove_argument(arg) self.remove_argument(arg)
if value is not False: if value is not False:
arg_str = arg if value is None else f'{arg}={value}' arg_str = arg if value is None else f'{arg}={value}'
self._arguments.append(arg_str) self._arguments.append(arg_str)
return self return self
def remove_argument(self, value): def remove_argument(self, value):
@ -220,7 +218,7 @@ class ChromiumOptions(object):
:param on_off: 开或关 :param on_off: 开或关
:return: 当前对象 :return: 当前对象
""" """
on_off = True if on_off else False on_off = None if on_off else False
return self.set_argument('--headless', on_off) return self.set_argument('--headless', on_off)
def set_no_imgs(self, on_off=True): def set_no_imgs(self, on_off=True):
@ -228,7 +226,7 @@ class ChromiumOptions(object):
:param on_off: 开或关 :param on_off: 开或关
:return: 当前对象 :return: 当前对象
""" """
on_off = True if on_off else False on_off = None if on_off else False
return self.set_argument('--blink-settings=imagesEnabled=false', on_off) return self.set_argument('--blink-settings=imagesEnabled=false', on_off)
def set_no_js(self, on_off=True): def set_no_js(self, on_off=True):
@ -236,7 +234,7 @@ class ChromiumOptions(object):
:param on_off: 开或关 :param on_off: 开或关
:return: 当前对象 :return: 当前对象
""" """
on_off = True if on_off else False on_off = None if on_off else False
return self.set_argument('--disable-javascript', on_off) return self.set_argument('--disable-javascript', on_off)
def set_mute(self, on_off=True): def set_mute(self, on_off=True):
@ -244,7 +242,7 @@ class ChromiumOptions(object):
:param on_off: 开或关 :param on_off: 开或关
:return: 当前对象 :return: 当前对象
""" """
on_off = True if on_off else False on_off = None if on_off else False
return self.set_argument('--mute-audio', on_off) return self.set_argument('--mute-audio', on_off)
def set_user_agent(self, user_agent): def set_user_agent(self, user_agent):

View File

@ -2,6 +2,7 @@
from configparser import RawConfigParser, NoSectionError, NoOptionError from configparser import RawConfigParser, NoSectionError, NoOptionError
from pathlib import Path from pathlib import Path
from pprint import pprint
class OptionsManager(object): class OptionsManager(object):
@ -100,3 +101,10 @@ class OptionsManager(object):
def save_to_default(self): def save_to_default(self):
"""保存当前配置到默认ini文件""" """保存当前配置到默认ini文件"""
return self.save('default') return self.save('default')
def show(self):
"""打印所有设置信息"""
for i in self._conf.sections():
print(f'[{i}]')
pprint(self.get_option(i))
print()

View File

@ -28,3 +28,5 @@ class OptionsManager(object):
def save(self, path: str = None) -> str: ... def save(self, path: str = None) -> str: ...
def save_to_default(self) -> str: ... def save_to_default(self) -> str: ...
def show(self) -> None: ...

View File

@ -5,32 +5,28 @@
""" """
from os import popen from os import popen
from pathlib import Path from pathlib import Path
from pprint import pprint
from re import search from re import search
from typing import Union from typing import Union
from selenium import webdriver from selenium import webdriver
from .functions.tools import unzip from .configs.chromium_options import ChromiumOptions
from .configs.options_manage import OptionsManager
from .configs.driver_options import DriverOptions from .configs.driver_options import DriverOptions
from .configs.options_manage import OptionsManager
from .drission import Drission from .drission import Drission
from .functions.tools import unzip
from .session_page import SessionPage from .session_page import SessionPage
def show_settings(ini_path=None): def show_settings(ini_path=None):
"""打印ini文件内容""" """打印ini文件内容
om = OptionsManager(ini_path) :param ini_path: ini文件路径
print('paths:') :return: None
pprint(om.get_option('paths')) """
print('\nchrome options:') OptionsManager(ini_path).show()
pprint(om.get_option('chrome_options'))
print('\nsession options:')
pprint(om.get_option('session_options'))
def set_paths(driver_path=None, def set_paths(driver_path=None,
chrome_path=None,
browser_path=None, browser_path=None,
local_port=None, local_port=None,
debugger_address=None, debugger_address=None,
@ -41,7 +37,6 @@ def set_paths(driver_path=None,
check_version=False): check_version=False):
"""快捷的路径设置函数 \n """快捷的路径设置函数 \n
:param driver_path: chromedriver.exe路径 :param driver_path: chromedriver.exe路径
:param chrome_path: chrome.exe路径
:param browser_path: 浏览器可执行文件路径 :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
@ -60,9 +55,6 @@ def set_paths(driver_path=None,
if driver_path is not None: if driver_path is not None:
om.set_item('paths', 'chromedriver_path', format_path(driver_path)) om.set_item('paths', 'chromedriver_path', format_path(driver_path))
if chrome_path is not None:
om.set_item('chrome_options', 'binary_location', format_path(chrome_path))
if browser_path is not None: if browser_path is not None:
om.set_item('chrome_options', 'binary_location', format_path(browser_path)) om.set_item('chrome_options', 'binary_location', format_path(browser_path))
@ -84,23 +76,18 @@ def set_paths(driver_path=None,
set_argument('--disk-cache-dir', format_path(cache_path), ini_path) set_argument('--disk-cache-dir', format_path(cache_path), ini_path)
if check_version: if check_version:
check_driver_version(format_path(driver_path), format_path(chrome_path)) check_driver_version(format_path(driver_path), format_path(browser_path))
def set_argument(arg, value, ini_path=None): def set_argument(arg, value=None, ini_path=None):
"""设置浏览器配置argument属性 \n """设置浏览器配置argument属性 \n
:param arg: 属性名 :param arg: 属性名
:param value: 属性值有值的属性传入值没有的传入bool :param value: 属性值有值的属性传入值没有的传入None
:param ini_path: 要修改的ini文件路径 :param ini_path: 要修改的ini文件路径
:return: None :return: None
""" """
do = DriverOptions(ini_path=ini_path) do = ChromiumOptions(ini_path=ini_path)
do.remove_argument(arg) do.set_argument(arg, value)
if value:
arg_str = arg if isinstance(value, bool) else f'{arg}={value}'
do.add_argument(arg_str)
do.save() do.save()
@ -110,7 +97,7 @@ def set_headless(on_off=True, ini_path=None):
:param ini_path: 要修改的ini文件路径 :param ini_path: 要修改的ini文件路径
:return: None :return: None
""" """
on_off = True if on_off else False on_off = None if on_off else False
set_argument('--headless', on_off, ini_path) set_argument('--headless', on_off, ini_path)
@ -120,7 +107,7 @@ def set_no_imgs(on_off=True, ini_path=None):
:param ini_path: 要修改的ini文件路径 :param ini_path: 要修改的ini文件路径
:return: None :return: None
""" """
on_off = True if on_off else False on_off = None if on_off else False
set_argument('--blink-settings=imagesEnabled=false', on_off, ini_path) set_argument('--blink-settings=imagesEnabled=false', on_off, ini_path)
@ -130,7 +117,7 @@ def set_no_js(on_off=True, ini_path=None):
:param ini_path: 要修改的ini文件路径 :param ini_path: 要修改的ini文件路径
:return: None :return: None
""" """
on_off = True if on_off else False on_off = None if on_off else False
set_argument('--disable-javascript', on_off, ini_path) set_argument('--disable-javascript', on_off, ini_path)
@ -140,7 +127,7 @@ def set_mute(on_off=True, ini_path=None):
:param ini_path: 要修改的ini文件路径 :param ini_path: 要修改的ini文件路径
:return: None :return: None
""" """
on_off = True if on_off else False on_off = None if on_off else False
set_argument('--mute-audio', on_off, ini_path) set_argument('--mute-audio', on_off, ini_path)
@ -150,7 +137,7 @@ def set_user_agent(user_agent, ini_path=None):
:param ini_path: 要修改的ini文件路径 :param ini_path: 要修改的ini文件路径
:return: None :return: None
""" """
set_argument('user-agent', user_agent, ini_path) set_argument('--user-agent', user_agent, ini_path)
def set_proxy(proxy, ini_path=None): def set_proxy(proxy, ini_path=None):

View File

@ -21,7 +21,7 @@ def set_paths(driver_path: str = None,
check_version: bool = False) -> None: ... check_version: bool = False) -> None: ...
def set_argument(arg: str, value: Union[bool, str], ini_path: str = None) -> None: ... def set_argument(arg: str, value: Union[bool, str] = None, ini_path: str = None) -> None: ...
def set_headless(on_off: bool = True, ini_path: str = None) -> None: ... def set_headless(on_off: bool = True, ini_path: str = None) -> None: ...

View File

@ -38,15 +38,14 @@ class SessionPage(BasePage):
:return: None :return: None
""" """
if Session_or_Options is None or isinstance(Session_or_Options, SessionOptions): if Session_or_Options is None or isinstance(Session_or_Options, SessionOptions):
options = Session_or_Options or SessionOptions() self._session_options = Session_or_Options or SessionOptions()
self._set_session(options) self._set_session(self._session_options)
self._timeout = options.timeout
self._download_path = options.download_path
elif isinstance(Session_or_Options, Session): elif isinstance(Session_or_Options, Session):
self._session = Session_or_Options self._session = Session_or_Options
self._timeout = 10 self._session_options = SessionOptions(read_file=False)
self._download_path = None
self._set_options()
def _set_session(self, opt): def _set_session(self, opt):
"""根据传入字典对session进行设置 \n """根据传入字典对session进行设置 \n
@ -70,6 +69,11 @@ class SessionPage(BasePage):
if attr: if attr:
self._session.__setattr__(i, attr) self._session.__setattr__(i, attr)
def _set_options(self):
"""设置WebPage中与d模式共用的配置便于WebPage覆盖掉"""
self._timeouts = self._session_options.timeout
self._download_path = self._session_options.download_path
def set_cookies(self, cookies): def set_cookies(self, cookies):
cookies = cookies_to_tuple(cookies) cookies = cookies_to_tuple(cookies)
for cookie in cookies: for cookie in cookies:

View File

@ -21,6 +21,7 @@ class SessionPage(BasePage):
session_or_options: Union[Session, SessionOptions] = None, session_or_options: Union[Session, SessionOptions] = None,
timeout: float = None): timeout: float = None):
self._session: Session = ... self._session: Session = ...
self._session_options: SessionOptions = ...
self._url: str = ... self._url: str = ...
self._response: Response = ... self._response: Response = ...
self._download_path: str = ... self._download_path: str = ...
@ -34,6 +35,8 @@ class SessionPage(BasePage):
def _set_session(self, opt: SessionOptions) -> None: ... def _set_session(self, opt: SessionOptions) -> None: ...
def _set_options(self) -> None: ...
def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
def set_headers(self, headers: dict) -> None: ... def set_headers(self, headers: dict) -> None: ...

View File

@ -110,6 +110,10 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
t = self._driver_options.timeouts t = self._driver_options.timeouts
self.set_timeouts(t['implicit'], t['pageLoad'], t['script']) self.set_timeouts(t['implicit'], t['pageLoad'], t['script'])
def _set_options(self):
"""覆盖父类同名方法"""
pass
def __call__(self, loc_or_str, timeout=None): def __call__(self, loc_or_str, timeout=None):
"""在内部查找元素 \n """在内部查找元素 \n
ele = page('@id=ele_id') \n ele = page('@id=ele_id') \n

View File

@ -172,6 +172,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
def _set_both_options(self, dr_opt: Union[ChromiumDriver, DriverOptions], def _set_both_options(self, dr_opt: Union[ChromiumDriver, DriverOptions],
se_opt: Union[Session, SessionOptions, dict, bool, None]) -> None: ... se_opt: Union[Session, SessionOptions, dict, bool, None]) -> None: ...
def _set_options(self) -> None: ...
def _set_driver_options(self, driver_or_Options: Union[ChromiumDriver, DriverOptions]) -> None: ... def _set_driver_options(self, driver_or_Options: Union[ChromiumDriver, DriverOptions]) -> None: ...
def _set_session_options(self, Session_or_Options: Union[Session, SessionOptions]) -> None: ... def _set_session_options(self, Session_or_Options: Union[Session, SessionOptions]) -> None: ...