更新README

This commit is contained in:
g1879 2020-07-20 18:15:42 +08:00
parent 04cb13265f
commit 745bc09427
2 changed files with 118 additions and 78 deletions

View File

@ -4,11 +4,11 @@
# Introduction
***
DrissionPage, the combination of driver and session, is a Python-based Web automation operation integration tool.
It integrates selenium and requests_html to achieve seamless switching between them.
Therefore, the convenience of selenium and the high efficiency of requests can be considered.
It encapsulates commonly used methods of page elements and is very suitable for the expansion of the PO mode of automated operation.
Even better, it is very concise and user-friendly, with little code and friendly to novices.
DrissionPage, the combination of driver and session, is a python-based Web automation operation integration tool.
It realizes the seamless switching between selenium and requests.
Therefore, the convenience of selenium and the high efficiency of requests can be balanced.
It uses POM mode to encapsulate common methods of page elements, which is very suitable for automatic operation function expansion.
What's even better is that its usage is very concise and user-friendly, with a small amount of code and friendly to novices.
**Project address**
@ -24,10 +24,9 @@ Even better, it is very concise and user-friendly, with little code and friendly
***
- Allows seamless switching between selenium and requests, sharing session.
- Use POM mode to encapsulate common methods for easy expansion.
- The two modes provide a unified operation method with consistent user experience.
- Common methods are encapsulated in units of pages to facilitate PO mode expansion.
- Humanized operation method of page elements to reduce the workload of page analysis and coding.
- Save configuration information to file for easy recall.
- Some common functions (such as click) have been optimized to better meet the actual needs.
- Easy configuration method to get rid of the cumbersome browser configuration.
@ -40,6 +39,7 @@ Even better, it is very concise and user-friendly, with little code and friendly
- DrissionPage takes concise code as the first pursuit, streamlines long statements and completely retains its functions.
- DrissionPage encapsulates many commonly used functions and is more convenient to use.
- The core of DrissionPage is a page class, which can directly derive subclass pages to adapt to various scenarios.
- Simple browser configuration method, get rid of tedious settings.
The following code implements exactly the same function, comparing the code amounts of the two:
@ -373,6 +373,45 @@ element.location # Returns the element position
## Chrome shortcut settings
The configuration of chrome is very cumbersome. In order to simplify the use, this library provides setting methods for common configurations.
### DriverOptions Object
The DriverOptions object inherits from the Options object of selenium.webdriver.chrome.options, and the following methods are added to it:
```python
remove_argument(value) # Delete an argument value
remove_experimental_option(key) # Delete an experimental_option setting
remove_all_extensions() # Delete all plugins
save() # Save the configuration to the default ini file
save('D:\\settings.ini') # Save to other path
set_argument(arg, value) # Set argument attribute
set_headless(on_off) # Set whether to use interfaceless mode
set_no_imgs(on_off) # Set whether to load pictures
set_no_js(on_off) # Set whether to disable js
set_mute(on_off) # Set whether to mute
set_user_agent(user_agent) # Set user agent
set_proxy(proxy) # Set proxy address
set_paths(driver_path, chrome_path, debugger_address, download_path, user_data_path, cache_path) # Set browser-related paths
```
### Instructions
```python
do = DriverOptions(read_file=False) # Create chrome configuration object, not read from ini file
do.set_headless(False) # Show browser interface
do.set_no_imgs(True) # Don't load pictures
do.set_paths(driver_path='D:\\chromedriver.exe', chrome_path='D:\\chrome.exe') # Set paths
drission = Drission(driver_options=do) # Create Drission object with configuration object
page = MixPage(drission) # Create a MixPage object with a Drission object
do.save() # Save the configuration to the default ini file
```
## Save configuration
Because chrome and headers have many configurations, an ini file is set up to save commonly used configurations. You can use the OptionsManager object to get and save the configuration, and use the DriverOptions object to modify the chrome configuration. You can also save multiple ini files and call them according to different projects.
@ -436,26 +475,6 @@ headers = {
}
```
### Usage example
```python
from DrissionPage import *
from DrissionPage.configs import *
driver_options = DriverOptions() # Read configuration from default ini file
driver_options = DriverOptions('D:\\settings.ini') # Read configuration from incoming ini file
driver_options.add_argument('--headless') # Add configuration
driver_options.remove_experimental_options('prefs') # Remove configuration
driver_options.save() # Save configuration
driver_options.save('D:\\settings.ini') # Save to other path
options_manager = OptionsManager() # Create OptionsManager object
driver_path = options_manager.get_value('paths', 'chromedriver_path') # Read path information
drission = Drission(driver_options, driver_path) # Create a Drission object using configuration
drission = Drission(ini_path = 'D:\\settings.ini') # Use other ini files to create objects
```
### OptionsManager object
The OptionsManager object is used to read, set, and save configurations.
@ -468,25 +487,27 @@ save() # Save configuration to default ini file
save('D:\\settings.ini') # Save to other path
```
** Note **: If you do not pass in the path when saving, it will be saved to the ini file in the module directory, even if you are not reading the default ini file.
### DriverOptions object
The DriverOptions object inherits from the Options object of selenium.webdriver.chrome.options, and adds the following methods to it:
### Usage example
```python
remove_argument(value) # Delete an argument value
remove_experimental_option(key) # Delete a experimental_option setting
remove_all_extensions() # Remove all plugins
save() # Save configuration to ini file
save('D:\\settings.ini') # Save to other path
from DrissionPage.configs import *
options_manager = OptionsManager() # Create OptionsManager object from default ini file
options_manager = OptionsManager('D:\\settings.ini') # Create OptionsManager object from other ini files
driver_path = options_manager.get_value('paths', 'chromedriver_path') # Read path information
options_manager.save() # Save to the default ini file
options_manager.save('D:\\settings.ini') # Save to other path
drission = Drission(ini_path = 'D:\\settings.ini') # Use other ini files to create objects
```
**Note** : If you do not pass in the path when saving, it will be saved to the ini file in the module directory, even if you are not reading the default ini file.
## easy_set methods
Chrome's configuration is hard to remember, so write the common configuration as a simple method that will modify the ini file.
Calling the easy_set method will modify the content of the default ini file.
```python
set_headless(True) # Set headless mode
@ -499,9 +520,7 @@ set_paths(paths) # See the Initialization section
set_argument(arg, on_off) # Set the property. If the property has no value (e.g. 'zh_CN.utf-8'), the value is bool representing the switch. If value is "" or False, delete the attribute entry
```
# PO mode
# POM mode
***

View File

@ -3,9 +3,9 @@
***
DrissionPage即driver和session的合体是个基于python的Web自动化操作集成工具。
整合了selenium和requests_html实现了它们之间的无缝切换。
实现了selenium和requests之间的无缝切换。
因此可以兼顾selenium的便利性和requests的高效率。
它封装了页面元素常用的方法,很适合自动化操作PO模式的扩展。
用POM模式封装了页面元素常用的方法,很适合自动化操作功能扩展。
更棒的是,它的使用方式非常简洁和人性化,代码量少,对新手友好。
**项目地址:**
@ -22,10 +22,9 @@ DrissionPage即driver和session的合体是个基于python的Web自动化
***
- 允许在selenium和requests间无缝切换共享session。
- 两种模式提供统一的操作方法,使用体验一致。
- 以页面为单位封装常用方法便于PO模式扩展。
- 使用POM模式封装常用方法便于扩展。
- 两种模式提供统一的操作方法,使用体验一致。
- 人性化的页面元素操作方法,减轻页面分析工作量和编码量。
- 把配置信息保存到文件,方便调用。
- 对某些常用功能(如点击)作了优化,更符合实际使用需要。
- 简易的配置方法,摆脱繁琐的浏览器配置。
@ -38,6 +37,7 @@ DrissionPage即driver和session的合体是个基于python的Web自动化
- DrissionPage以简洁的代码为第一追求对冗长的语句做了精简并完全保留了其功能。
- DrissionPage封装了许多常用功能使用更便捷。
- DrissionPage的核心是个页面类可直接派生子类页面适应各种场景须要。
- 简易的浏览器配置方法,摆脱繁琐的设置。
以下代码实现一模一样的功能,对比两者的代码量:
@ -371,6 +371,45 @@ element.location # 元素位置
## Chrome快捷设置
chrome的配置很繁琐为简化使用本库提供了常用配置的设置方法。
### DriverOptions对象
DriverOptions对象继承自selenium.webdriver.chrome.options的Options对象在其基础上增加了以下方法
```python
remove_argument(value) # 删除某argument值
remove_experimental_option(key) # 删除某experimental_option设置
remove_all_extensions() # 删除全部插件
save() # 保存配置到默认ini文件
save('D:\\settings.ini') # 保存到其它路径
set_argument(arg, value) # 设置argument属性
set_headless(on_off) # 设置是否使用无界面模式
set_no_imgs(on_off) # 设置是否加载图片
set_no_js(on_off) # 设置是否禁用js
set_mute(on_off) # 设置是否静音
set_user_agent(user_agent) # 设置user agent
set_proxy(proxy) # 设置代理地址
set_paths(driver_path, chrome_path, debugger_address, download_path, user_data_path, cache_path) # 设置浏览器相关的路径
```
### 使用方法
```python
do = DriverOptions(read_file=False) # 创建chrome配置对象不从ini文件读取
do.set_headless(False) # 显示浏览器界面
do.set_no_imgs(True) # 不加载图片
do.set_paths(driver_path='D:\\chromedriver.exe', chrome_path='D:\\chrome.exe') # 设置路径
drission = Drission(driver_options=do) # 用配置对象创建Drission对象
page = MixPage(drission) # 用Drission对象创建MixPage对象
do.save() # 保存配置到默认ini文件
```
## 保存配置
因chrome和headers配置繁多故设置一个ini文件专门用于保存常用配置你可使用OptionsManager对象获取和保存配置用DriverOptions对象修改chrome配置。你也可以保存多个ini文件按不同项目须要调用。
@ -434,26 +473,6 @@ headers = {
}
```
### 使用示例
```python
from DrissionPage import *
from DrissionPage.configs import *
driver_options = DriverOptions() # 从默认ini文件读取配置
driver_options = DriverOptions('D:\\settings.ini') # 从传入的ini文件读取配置
driver_options.add_argument('--headless') # 添加配置
driver_options.remove_experimental_options('prefs') # 移除配置
driver_options.save() # 保存配置
driver_options.save('D:\\settings.ini') # 保存到其它路径
options_manager = OptionsManager() # 创建OptionsManager对象
driver_path = options_manager.get_value('paths', 'chromedriver_path') # 读取路径信息
drission = Drission(driver_options, driver_path) # 使用配置创建Drission对象
drission = Drission(ini_path = 'D:\\settings.ini') # 使用其它ini文件创建对象
```
### OptionsManager对象
OptionsManager对象用于读取、设置和保存配置。
@ -466,25 +485,27 @@ save() # 保存配置到默认ini文件
save('D:\\settings.ini') # 保存到其它路径
```
**注意**保存时若不传入路径会保存到模块目录下的ini文件即使读取的不是默认ini文件也一样。
### DriverOptions对象
DriverOptions对象继承自selenium.webdriver.chrome.options的Options对象在其基础上增加了以下方法
### 使用示例
```python
remove_argument(value) # 删除某argument值
remove_experimental_option(key) # 删除某experimental_option设置
remove_all_extensions() # 删除全部插件
save() # 保存配置到ini文件
save('D:\\settings.ini') # 保存到其它路径
from DrissionPage.configs import *
options_manager = OptionsManager() # 从默认ini文件创建OptionsManager对象
options_manager = OptionsManager('D:\\settings.ini') # 从其它ini文件创建OptionsManager对象
driver_path = options_manager.get_value('paths', 'chromedriver_path') # 读取路径信息
options_manager.save() # 保存到默认ini文件
options_manager.save('D:\\settings.ini') # 保存到其它路径
drission = Drission(ini_path = 'D:\\settings.ini') # 使用其它ini文件创建对象
```
**注意**保存时若不传入路径会保存到模块目录下的ini文件即使读取的不是默认ini文件也一样。
## easy_set方法
chrome的配置太难记所以把常用的配置写成简单的方法调用会修改ini文件相关内容。
调用easy_set方法会修改默认ini文件相关内容。
```python
set_headless(True) # 开启headless模式
@ -497,7 +518,7 @@ set_paths(paths) # 见 [初始化] 一节
set_argument(arg, value) # 设置属性,若属性无值(如'zh_CN.UTF-8'value为bool表示开关否则value为str当value为''或False删除该属性项
```
# PO模式
# POM模式
***