diff --git a/DrissionPage/config.py b/DrissionPage/config.py index 6f15016..95fdce6 100644 --- a/DrissionPage/config.py +++ b/DrissionPage/config.py @@ -24,7 +24,7 @@ class OptionsManager(object): self._conf = ConfigParser() self._conf.read(self.path, encoding='utf-8') if 'global_tmp_path' not in self.get_option('paths') or not self.get_value('paths', 'global_tmp_path'): - global_tmp_path = f'{str(Path(__file__).parent)}\\tmp' + global_tmp_path = str((Path(__file__).parent / 'tmp').absolute()) Path(global_tmp_path).mkdir(parents=True, exist_ok=True) self.set_item('paths', 'global_tmp_path', global_tmp_path) self.save() @@ -59,7 +59,7 @@ class OptionsManager(object): :return: None """ path = path or Path(__file__).parent / 'configs.ini' - self._conf.write(open(path, 'w')) + self._conf.write(open(path, 'w', encoding='utf-8')) class DriverOptions(Options): diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index 3585533..3fbaf9b 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -20,11 +20,10 @@ from .driver_element import DriverElement, execute_driver_find class DriverPage(object): """DriverPage封装了页面操作的常用功能,使用selenium来获取、解析、操作网页""" - def __init__(self, driver: WebDriver, timeout: float = 10): # , locs=None + def __init__(self, driver: WebDriver, timeout: float = 10): """初始化函数,接收一个WebDriver对象,用来操作网页""" self._driver = driver self.timeout = timeout - # self._locs = locs self._url = None self._url_available = None @@ -225,14 +224,11 @@ class DriverPage(object): alertObject = self.driver.switch_to.alert except NoAlertPresentException: return None - if text: alertObject.send_keys(text) - text = alertObject.text if mode == 'cancel': alertObject.dismiss() elif mode == 'ok': alertObject.accept() - return text diff --git a/README.en.md b/README.en.md index 70a8b5e..1ed5c03 100644 --- a/README.en.md +++ b/README.en.md @@ -39,28 +39,31 @@ The design concept of this library is to keep everything simple, try to provide *** -Example: Log in to the website with selenium, then switch to requests to read the web page, and print the element attributes. +Example: Log in to the website with selenium, then switch to requests to read the web page. ```python -from DrissionPage import * -from time import sleep - drission = Drission() # Create Drive Object page = MixPage(drission) # Create page object, default driver mode -page.get('https://gitee.com/profile') # Visit the personal center page (not logged in, redirect to the login page) +page.get('https://gitee.com/profile') # Visit personal center page (redirect to the login page) -# Use selenium to enter the account password to log in -page.ele('@id:user_login').input('your_user_name') +page.ele('@id:user_login').input('your_user_name') # Use selenium to log in page.ele('@id:user_password').input('your_password\n') -sleep(1) # Wait for login page.change_mode() # Switch to session mode - print('Title after login:', page.title, '\n') # Output of session mode after login +``` -# Get and print attributes +Output: + +``` +Title after login: Dashboard - Gitee +``` + +Example: Find element and print attributes. + +```python foot = page.ele('@id:footer-left') # Find elements by id -first_col = foot.ele('css:>div') # Find first div element in the lower level of foot element by css selector. +first_col = foot.ele('css:>div') # Find first div element in the lower level by css selector. lnk = first_col.ele('text:Git Branching') # Find elements by text content text = lnk.text # Get element text href = lnk.attr('href') # Get element attribute value @@ -72,8 +75,6 @@ print(text, href) Output: ``` -Title after login: Dashboard - Gitee - Learn Git Branching https://oschina.gitee.io/learn-git-branching/ ``` diff --git a/README.zh-cn.md b/README.zh-cn.md index 61bf7b2..53d6eb2 100644 --- a/README.zh-cn.md +++ b/README.zh-cn.md @@ -41,26 +41,28 @@ DrissionPage,即driver和session的合体,是个基于python的Web自动化 *** -例:用selenium登录网站,然后切换到requests读取网页,打印元素属性。 +例:用selenium登录网站,然后切换到requests读取网页。 ```python -from DrissionPage import * -from time import sleep - drission = Drission() # 创建驱动器对象 page = MixPage(drission) # 创建页面对象,默认driver模式 page.get('https://gitee.com/profile') # 访问个人中心页面(未登录,重定向到登录页面) -# 使用selenium输入账号密码登录 -page.ele('@id:user_login').input('your_user_name') +page.ele('@id:user_login').input('your_user_name') # 使用selenium输入账号密码登录 page.ele('@id:user_password').input('your_password\n') -sleep(1) # 等待登录 page.change_mode() # 切换到session模式 - print('登录后title:', page.title, '\n') # 登录后session模式的输出 +``` -# 获取并打印属性 +输出: + +``` +登录后title: 个人资料 - 码云 Gitee.com +``` + +例:获取并打印属性 +```python foot = page.ele('@id:footer-left') # 用id查找元素 first_col = foot.ele('css:>div') # 使用css selector在元素的下级中查找元素(第一个) lnk = first_col.ele('text:命令学') # 使用文本内容查找元素 @@ -74,8 +76,6 @@ print(text, href) 输出: ``` -登录后title: 个人资料 - 码云 Gitee.com - Git 命令学习 https://oschina.gitee.io/learn-git-branching/ ```