mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
增加set_headless方法
to_iframe()兼容selenium参数
This commit is contained in:
parent
09c98d8cf6
commit
afacbbe775
@ -135,18 +135,33 @@ class DriverPage(object):
|
||||
self.close_current_tab()
|
||||
self.driver.switch_to.window(page_handle) # 把权柄定位回保留的页面
|
||||
|
||||
def to_iframe(self, loc_or_ele: Union[str, tuple, WebElement] = 'main') -> bool:
|
||||
"""跳转到iframe,若传入字符串main则跳转到最高级"""
|
||||
if loc_or_ele == 'main':
|
||||
self.driver.switch_to.default_content()
|
||||
return True
|
||||
else:
|
||||
ele = self.ele(loc_or_ele)
|
||||
try:
|
||||
def to_iframe(self, loc_or_ele: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> None:
|
||||
"""跳转到iframe
|
||||
:param loc_or_ele: 可接收iframe序号(0开始)、id或name、控制字符串、loc tuple、WebElement对象、DriverElement对象,
|
||||
传入'main'则跳到最高层
|
||||
:return: None
|
||||
"""
|
||||
if isinstance(loc_or_ele, int):
|
||||
# 根据序号跳转
|
||||
self.driver.switch_to.frame(loc_or_ele)
|
||||
elif isinstance(loc_or_ele, str):
|
||||
if loc_or_ele == 'main':
|
||||
# 跳转到最上级
|
||||
self.driver.switch_to.default_content()
|
||||
elif ':' not in loc_or_ele:
|
||||
# 传入id或name
|
||||
self.driver.switch_to.frame(loc_or_ele)
|
||||
else:
|
||||
# 传入控制字符串
|
||||
ele = self.ele(loc_or_ele)
|
||||
self.driver.switch_to.frame(ele.inner_ele)
|
||||
return True
|
||||
except:
|
||||
raise
|
||||
elif isinstance(loc_or_ele, WebElement):
|
||||
self.driver.switch_to.frame(loc_or_ele)
|
||||
elif isinstance(loc_or_ele, DriverElement):
|
||||
self.driver.switch_to.frame(loc_or_ele.inner_ele)
|
||||
elif isinstance(loc_or_ele, tuple):
|
||||
ele = self.ele(loc_or_ele)
|
||||
self.driver.switch_to.frame(ele.inner_ele)
|
||||
|
||||
def screenshot(self, path: str = None, filename: str = None) -> str:
|
||||
"""获取网页截图"""
|
||||
|
@ -5,7 +5,6 @@
|
||||
@File : driver_page.py
|
||||
"""
|
||||
from selenium import webdriver
|
||||
import re
|
||||
|
||||
from DrissionPage.config import OptionsManager, DriverOptions
|
||||
|
||||
@ -14,13 +13,15 @@ def set_paths(driver_path: str = None,
|
||||
chrome_path: str = None,
|
||||
debugger_address: str = None,
|
||||
global_tmp_path: str = None,
|
||||
download_path: str = None) -> None:
|
||||
download_path: str = None,
|
||||
check_version: bool = True) -> None:
|
||||
"""简易设置路径函数
|
||||
:param driver_path: chromedriver.exe路径
|
||||
:param chrome_path: chrome.exe路径
|
||||
:param debugger_address: 调试浏览器地址,例:127.0.0.1:9222
|
||||
:param download_path: 下载文件路径
|
||||
:param global_tmp_path: 临时文件夹路径
|
||||
:param check_version: 是否检查chromedriver和chrome是否匹配
|
||||
:return: None
|
||||
"""
|
||||
om = OptionsManager()
|
||||
@ -37,10 +38,24 @@ def set_paths(driver_path: str = None,
|
||||
experimental_options['prefs']['download.default_directory'] = download_path
|
||||
om.set_item('chrome_options', 'experimental_options', experimental_options)
|
||||
om.save()
|
||||
check_driver_version(driver_path, chrome_path)
|
||||
if check_version:
|
||||
check_driver_version(driver_path, chrome_path)
|
||||
|
||||
|
||||
def set_headless(on_off: bool) -> None:
|
||||
"""设置headless"""
|
||||
do = DriverOptions()
|
||||
if on_off:
|
||||
if '--headless' not in do.arguments:
|
||||
do.add_argument('--headless')
|
||||
else:
|
||||
do.remove_argument('--headless')
|
||||
do.save()
|
||||
|
||||
|
||||
def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bool:
|
||||
"""检查传入的chrome和chromedriver是否匹配"""
|
||||
print('正在检测可用性...')
|
||||
om = OptionsManager()
|
||||
driver_path = driver_path or om.get_value('paths', 'chromedriver_path') or 'chromedriver'
|
||||
chrome_path = chrome_path or om.get_value('chrome_options', 'binary_location')
|
||||
@ -54,12 +69,9 @@ def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bo
|
||||
print('版本匹配,可正常使用。')
|
||||
return True
|
||||
except Exception as e:
|
||||
r = re.search(r'chromedriver=(.+?) ', str(e))
|
||||
info = f'''
|
||||
版本不兼容。
|
||||
请下载与当前chrome版本匹配的chromedriver。
|
||||
当前chromedriver版本:{r.group(1)}
|
||||
查看chrome版本方法:帮助 -> 关于Google Chrome
|
||||
出现异常:
|
||||
{e}
|
||||
chromedriver下载网址:https://chromedriver.chromium.org/downloads
|
||||
'''
|
||||
print(info)
|
||||
|
28
README.en.md
28
README.en.md
@ -130,10 +130,9 @@ This method also checks if the chrome and chromedriver versions match, and displ
|
||||
|
||||
or
|
||||
|
||||
版本不兼容。
|
||||
请下载与当前chrome版本匹配的chromedriver。
|
||||
当前chromedriver版本:<你的chromedriver版本号>
|
||||
查看chrome版本方法:帮助 -> 关于Google Chrome
|
||||
出现异常:
|
||||
Message: session not created: Chrome version must be between 70 and 73
|
||||
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.19631 x86_64)
|
||||
chromedriver下载网址:https://chromedriver.chromium.org/downloads
|
||||
```
|
||||
|
||||
@ -795,17 +794,19 @@ Parameter Description:
|
||||
|
||||
### to_iframe
|
||||
|
||||
to_iframe(loc_or_ele: Union[str, tuple, WebElement] = 'main') -> bool
|
||||
to_iframe(self, loc_or_ele: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> None
|
||||
|
||||
Jump to iframe, default jump to the highest level.
|
||||
Jump to iframe, compatible with selenium native parameters.
|
||||
|
||||
Parameter Description:
|
||||
|
||||
- loc_or_ele - The search condition of the iframe element is the same as the search condition of the ele () method.
|
||||
- loc_or_ele - To search for iframe element conditions, you can receive iframe serial number (starting at 0), id or name, control string, loc parameter, WebElement object, DriverElement object, and pass 'main' to jump to the top level.
|
||||
|
||||
Examples:
|
||||
- to_iframe('@id:iframe_id')
|
||||
- to_iframe(iframe_element)
|
||||
- to_iframe(0)
|
||||
- to_iframe('iframe_name')
|
||||
|
||||
### scroll_to_see
|
||||
|
||||
@ -1302,7 +1303,7 @@ Parameter Description:
|
||||
|
||||
### set_paths
|
||||
|
||||
set_paths(driver_path: str = None, chrome_path: str = None, debugger_address: str = None, global_tmp_path: str = None, download_path: str = None) -> None
|
||||
set_paths(driver_path: str = None, chrome_path: str = None, debugger_address: str = None, global_tmp_path: str = None, download_path: str = None, check_version: bool = True) -> None
|
||||
|
||||
Convenient way to set the path, save the incoming path to the default ini file, and check whether the chrome and chromedriver versions match.
|
||||
|
||||
@ -1313,6 +1314,17 @@ Parameter Description:
|
||||
- debugger_address - Debug browser address, for example: 127.0.0.1:9222
|
||||
- download_path - Download path
|
||||
- global_tmp_path - Temporary folder path
|
||||
- check_version - Whether to check whether chromedriver and chrome match
|
||||
|
||||
### set_headless
|
||||
|
||||
set_headless(on_off: bool) -> None
|
||||
|
||||
Convenient headless switch.
|
||||
|
||||
Parameter Description:
|
||||
|
||||
- on_off - Whether to enable headless mode
|
||||
|
||||
### check_driver_version
|
||||
|
||||
|
@ -37,10 +37,16 @@ DrissionPage,即driver和session的合体,是个基于python的Web自动化
|
||||
- 把配置信息保存到文件,方便调用。
|
||||
- 对某些常用功能(如点击)作了优化,更符合实际使用需要。
|
||||
|
||||
# 简单演示
|
||||
# 示例
|
||||
|
||||
***
|
||||
|
||||
## 示例网址
|
||||
|
||||
[使用DrissionPage爬取常见网站](https://github.com/g1879/DrissionPage-examples)
|
||||
|
||||
## 简单演示
|
||||
|
||||
例:用selenium登录网站,然后切换到requests读取网页。
|
||||
|
||||
```python
|
||||
@ -131,10 +137,9 @@ set_paths(driver_path, chrome_path)
|
||||
|
||||
或
|
||||
|
||||
版本不兼容。
|
||||
请下载与当前chrome版本匹配的chromedriver。
|
||||
当前chromedriver版本:<你的chromedriver版本号>
|
||||
查看chrome版本方法:帮助 -> 关于Google Chrome
|
||||
出现异常:
|
||||
Message: session not created: Chrome version must be between 70 and 73
|
||||
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.19631 x86_64)
|
||||
chromedriver下载网址:https://chromedriver.chromium.org/downloads
|
||||
```
|
||||
|
||||
@ -797,17 +802,19 @@ MixPage封装了页面操作的常用功能,可在driver和session模式间无
|
||||
|
||||
### to_iframe
|
||||
|
||||
to_iframe(loc_or_ele: Union[str, tuple, WebElement] = 'main') -> bool
|
||||
to_iframe(self, loc_or_ele: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> None
|
||||
|
||||
跳转到iframe,默认跳转到最高层级。
|
||||
跳转到iframe,默认跳转到最高层级,兼容selenium原生参数。
|
||||
|
||||
参数说明:
|
||||
|
||||
- loc_or_ele - 查找iframe元素的条件,和ele()方法的查找条件一致。
|
||||
- loc_or_ele - 查找iframe元素的条件,可接收iframe序号(0开始)、id或name、控制字符串、loc参数、WebElement对象、DriverElement对象,传入'main'则跳到最高层。
|
||||
|
||||
示例:
|
||||
- to_iframe('@id:iframe_id')
|
||||
- to_iframe(iframe_element)
|
||||
- to_iframe(0)
|
||||
- to_iframe('iframe_name')
|
||||
|
||||
### scroll_to_see
|
||||
|
||||
@ -1304,7 +1311,7 @@ session模式的元素对象,包装了一个Element对象,并封装了常用
|
||||
|
||||
### set_paths
|
||||
|
||||
set_paths(driver_path: str = None, chrome_path: str = None, debugger_address: str = None, global_tmp_path: str = None, download_path: str = None) -> None
|
||||
set_paths(driver_path: str = None, chrome_path: str = None, debugger_address: str = None, global_tmp_path: str = None, download_path: str = None, check_version: bool = True) -> None
|
||||
|
||||
便捷的设置路径方法,把传入的路径保存到默认ini文件,并检查chrome和chromedriver版本是否匹配。
|
||||
|
||||
@ -1315,6 +1322,17 @@ session模式的元素对象,包装了一个Element对象,并封装了常用
|
||||
- debugger_address - 调试浏览器地址,例:127.0.0.1:9222
|
||||
- download_path - 下载文件路径
|
||||
- global_tmp_path - 临时文件夹路径
|
||||
- check_version - 是否检查chromedriver和chrome是否匹配
|
||||
|
||||
### set_headless
|
||||
|
||||
set_headless(on_off: bool) -> None
|
||||
|
||||
便捷的headless开关。
|
||||
|
||||
参数说明:
|
||||
|
||||
- on_off - 是否开启headless模式
|
||||
|
||||
### check_driver_version
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user