增加easy_set功能

This commit is contained in:
g1879 2020-05-23 00:59:01 +08:00
parent c9502f6cfd
commit 3861cc57cf
4 changed files with 166 additions and 43 deletions

View File

@ -6,26 +6,26 @@ global_tmp_path =
debugger_address =
binary_location =
arguments = [
'--headless',
'--no-sandbox',
'--disable-gpu'
]
'--headless',
'--no-sandbox',
'--disable-gpu'
]
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"}],
'excludeSwitches': ["ignore-certificate-errors", "enable-automation"],
'useAutomationExtension': False
}
}
'prefs': {
'profile.default_content_settings.popups': 0,
'profile.default_content_setting_values': {'notifications': 2},
'plugins.plugins_list': [{"enabled": False, "name": "Chrome PDF Viewer"}],
'excludeSwitches': ["ignore-certificate-errors", "enable-automation"],
'useAutomationExtension': False
}
}
[session_options]
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Connection": "keep-alive",
"Accept-Charset": "utf-8;q=0.7,*;q=0.7"
}
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Connection": "keep-alive",
"Accept-Charset": "utf-8;q=0.7,*;q=0.7"
}

66
DrissionPage/easy_set.py Normal file
View File

@ -0,0 +1,66 @@
# -*- coding:utf-8 -*-
"""
@Author : g1879
@Contact : g1879@qq.com
@File : driver_page.py
"""
from selenium import webdriver
import re
from DrissionPage.config import OptionsManager, DriverOptions
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:
"""简易设置路径函数
: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: 临时文件夹路径
:return: None
"""
om = OptionsManager()
if driver_path is not None:
om.set_item('paths', 'chromedriver_path', driver_path)
if chrome_path is not None:
om.set_item('chrome_options', 'binary_location', chrome_path)
if debugger_address is not None:
om.set_item('chrome_options', 'debugger_address', debugger_address)
if global_tmp_path is not None:
om.set_item('paths', 'global_tmp_path', global_tmp_path)
if download_path is not None:
experimental_options = om.get_value('chrome_options', 'experimental_options')
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)
def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bool:
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')
do = DriverOptions(read_file=False)
do.add_argument('--headless')
if chrome_path:
do.binary_location = chrome_path
try:
driver = webdriver.Chrome(driver_path, options=do)
driver.quit()
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
chromedriver下载网址:https://chromedriver.chromium.org/downloads
'''
print(info)
return False

107
README.md
View File

@ -1,12 +1,11 @@
# 简介
***
DrissionPage,即driver和session的合体。
它是一个python库,是个Web自动化操作集成工具。
DrissionPage,即driver和session的合体,是个基于python的Web自动化操作集成工具。
它整合了selenium和requests_html,实现了它们之间的无缝切换。
因此可以兼顾selenium的便利性和requests的高效率。
它封装了页面元素常用的方法,很适合自动化操作PO模式的扩展。
更棒的是,它的使用方式非常人性化,代码量少,对新手友好。
更棒的是,它的使用方式非常简洁和人性化,代码量少,对新手友好。
# 背景
@ -18,6 +17,8 @@ DrissionPage,即driver和session的合体。
除了合并两者,本库还以网页为单位封装了常用功能,简化了selenium的操作和语句,在用于网页自动化操作时,减少考虑细节,专注功能实现,使用更方便。
本人学习过程中踩了很多坑,因此这个库的设计理念是一切从简,尽量提供简单直接的使用方法,对新手更友好。
**项目地址:**
- https://github.com/g1879/DrissionPage
@ -33,6 +34,7 @@ DrissionPage,即driver和session的合体。
- 两种模式提供统一的操作方法,使用体验一致。
- 以页面为单位封装常用方法,便于PO模式扩展。
- 人性化的页面元素操作方法,减轻页面分析工作量和编码量。
- 把配置信息保存到文件,方便调用。
- 对某些常用功能(如点击)作了优化,更符合实际使用需要。
# 简单演示
@ -103,32 +105,54 @@ from DrissionPage import *
## 初始化
使用selenium前,必须告诉它chrome.exe和chromedriver.exe的路径。
使用selenium前,必须配置chrome.exe和chromedriver.exe的路径,并确保它们版本匹配。
如果你已经很清楚selenium配置,或只使用session模式,可跳过这段。
如果你只使用session模式,可跳过本节。
配置路径有三种方法:
- 将两个路径路径写入系统变量。
- 使用时手动传入路径。
- 将路径写入本库的ini文件,以下详细说明。
- 将路径写入本库的ini文件(推荐)。
本库维护了一个ini文件,在第一次使用本库前,运行以下代码,会把这两个路径记录到ini文件中,以后再使用就不用重复传入。
若你选择第三种方式,请在第一次使用本库前,运行这几行代码,把这两个路径记录到ini文件中。
```python
from DrissionPage.config import OptionsManager # 导入配置管理包
options = OptionsManager() # 创建配置管理对象
driver_path = 'C:\\chrome\\chromedriver.exe' # 你的driver_path路径
chrome_path = 'D:\\chrome\\chrome.exe' # 你的chrome.exe路径
options.set_item('paths', 'chromedriver_path', driver_path) # 设置driver_path路径
options.set_item('chrome_options', 'binary_location', chrome_path) # 设置chrome.exe路径
options.save() # 保存到默认ini文件
from DrissionPage.easy_set import set_paths
driver_path = 'C:\\chrome\\chromedriver.exe' # 你的driver_path路径,可选
chrome_path = 'D:\\chrome\\chrome.exe' # 你的chrome.exe路径,可选
set_paths(driver_path, chrome_path)
```
注:不同项目可能须要不同版本的chrome和chromedriver,你还可保存多个ini文件,按须使用。
该方法还会检查chrome和chromedriver版本是否匹配,显示:
```
版本匹配,可正常使用。
或
版本不兼容。
请下载与当前chrome版本匹配的chromedriver。
当前chromedriver版本:<你的chromedriver版本号>
查看chrome版本方法:帮助 -> 关于Google Chrome
chromedriver下载网址:https://chromedriver.chromium.org/downloads
```
检查通过后,即可正常使用driver模式。
除了上述两个路径,该方法还可以设置以下路径:
```python
debugger_address # 调试浏览器地址,如:127.0.0.1:9222
download_path # 下载文件路径
global_tmp_path # 临时文件夹路径
```
Tips:
- 不同项目可能须要不同版本的chrome和chromedriver,你还可保存多个ini文件,按须使用。
- 推荐使用绿色版chrome,并手动设置路径,比较浏览器升级造成与chromedriver版本不匹配。
- 调试项目时推荐设置debugger_address,使用手动打开的浏览器调试,省时省力。
@ -137,10 +161,14 @@ options.save() # 保存到默认ini文件
Drission对象用于管理driver和session对象。可直接读取ini文件配置信息创建,也可以在初始化时传入配置信息。
```python
# 读取ini文件创建
# 由ini文件创建
drission = Drission()
```
# 用传入的配置信息创建
若要手动传入配置:
```python
# 用传入的配置信息创建(忽略ini文件)
from DrissionPage.config import DriverOptions
driver_options = DriverOptions() # 创建driver配置对象
@ -148,14 +176,14 @@ driver_options.binary_location = 'D:\\chrome\\chrome.exe' # chrome.exe路径
session_options = {'headers': {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)'}}
driver_path = 'C:\\chrome\\chromedriver.exe' # driver_path路径
drission = Drission(driver_options, session_options, driver_path)
drission = Drission(driver_options, session_options, driver_path) # 传入配置
```
## 使用页面对象MixPage
页面对象封装了常用的网页操作,并实现driver和session模式之间的切换。
MixPage页面对象封装了常用的网页操作,并实现driver和session模式之间的切换。
```python
page = MixPage(drission) # 默认driver模式
@ -178,7 +206,7 @@ page.scrool_to_see(element) # 滚动直到某元素可见
# 详见APIs...
```
注:调用只属于driver模式的方法,会自动切换到driver模式。
Tips:调用只属于driver模式的方法,会自动切换到driver模式。
@ -261,7 +289,7 @@ element.location # 元素位置
因chrome和headers配置繁多,故设置一个ini文件专门用于保存常用配置,你可使用OptionsManager对象获取和保存配置,用DriverOptions对象修改chrome配置。你也可以保存多个ini文件,按不同项目须要调用。
注:建议把常用配置文件保存到别的路径,以防本库升级时配置被重置。
Tips:建议把常用配置文件保存到别的路径,以防本库升级时配置被重置。
### ini文件内容
@ -1235,4 +1263,33 @@ session模式的元素对象,包装了一个Element对象,并封装了常用
​ 参数说明:
- path - ini文件的路径,默认保存到模块文件夹下的
- path - ini文件的路径,默认保存到模块文件夹下的
## 有用的方法
### 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
​ 便捷的设置路径方法,把传入的路径保存到默认ini文件,并检查chrome和chromedriver版本是否匹配。
​ 参数说明:
- driver_path - chromedriver.exe路径
- chrome_path - chrome.exe路径
- debugger_address - 调试浏览器地址,例:127.0.0.1:9222
- download_path - 下载文件路径
- global_tmp_path - 临时文件夹路径
### check_driver_version
​ check_driver_version(driver_path: str = None, chrome_path: str = None) -> bool
​ 检查chrome与chromedriver版本是否匹配。
​ 参数说明:
- driver_path - chromedriver.exe路径
- chrome_path - chrome.exe路径

View File

@ -8,7 +8,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="0.8.3",
version="0.8.4",
author="g1879",
author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.",