From 59d9f19cf825d5f486f37cbe690b48936d950275 Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 1 Mar 2023 17:35:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=B7=A5=E5=85=B7=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=88=B0common=E4=B8=8B=EF=BC=9B=E5=A2=9E=E5=8A=A0By?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 4 ---- DrissionPage/{tools.py => common.py} | 5 ++++- DrissionPage/commons/by.py | 10 ++++++++++ DrissionPage/commons/locator.py | 21 +++++++++++---------- 4 files changed, 25 insertions(+), 15 deletions(-) rename DrissionPage/{tools.py => common.py} (64%) create mode 100644 DrissionPage/commons/by.py diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 82e1b7d..335c6a4 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -12,10 +12,6 @@ from .web_page import WebPage from .configs.chromium_options import ChromiumOptions from .configs.session_options import SessionOptions -# 常用工具 -from .action_chains import ActionChains -from .commons.keys import Keys - # 旧版页面类和启动配置类 try: from .mixpage.mix_page import MixPage diff --git a/DrissionPage/tools.py b/DrissionPage/common.py similarity index 64% rename from DrissionPage/tools.py rename to DrissionPage/common.py index a77e11f..9e2ba98 100644 --- a/DrissionPage/tools.py +++ b/DrissionPage/common.py @@ -7,4 +7,7 @@ from FlowViewer import Listener, RequestMan from .session_element import make_session_ele -from .easy_set import get_match_driver + +from .action_chains import ActionChains +from .commons.keys import Keys +from .commons.by import By diff --git a/DrissionPage/commons/by.py b/DrissionPage/commons/by.py new file mode 100644 index 0000000..899a183 --- /dev/null +++ b/DrissionPage/commons/by.py @@ -0,0 +1,10 @@ +# -*- coding:utf-8 -*- +class By: + ID = 'id' + XPATH = 'xpath' + LINK_TEXT = 'link text' + PARTIAL_LINK_TEXT = 'partial link text' + NAME = 'name' + TAG_NAME = 'tag name' + CLASS_NAME = 'class name' + CSS_SELECTOR = 'css selector' diff --git a/DrissionPage/commons/locator.py b/DrissionPage/commons/locator.py index bc8a936..1ffeb2a 100644 --- a/DrissionPage/commons/locator.py +++ b/DrissionPage/commons/locator.py @@ -4,6 +4,7 @@ @Contact : g1879@qq.com """ from re import split +from .by import By def get_loc(loc, translate_css=False): @@ -219,32 +220,32 @@ def translate_loc(loc): if len(loc) != 2: raise ValueError('定位符长度必须为2。') - loc_by = 'xpath' + loc_by = By.XPATH loc_0 = loc[0].lower() - if loc_0 == 'xpath': + if loc_0 == By.XPATH: loc_str = loc[1] - elif loc_0 == 'css selector': + elif loc_0 == By.CSS_SELECTOR: loc_by = loc_0 loc_str = loc[1] - elif loc_0 == 'id': + elif loc_0 == By.ID: loc_str = f'//*[@id="{loc[1]}"]' - elif loc_0 == 'class name': + elif loc_0 == By.CLASS_NAME: loc_str = f'//*[@class="{loc[1]}"]' - elif loc_0 == 'link text': + elif loc_0 == By.PARTIAL_LINK_TEXT: loc_str = f'//a[text()="{loc[1]}"]' - elif loc_0 == 'name': + elif loc_0 == By.NAME: loc_str = f'//*[@name="{loc[1]}"]' - elif loc_0 == 'tag name': - loc_str = f'//{loc[1]}' + elif loc_0 == By.TAG_NAME: + loc_str = f'//*[name()="{loc[1]}"]' - elif loc_0 == 'partial link text': + elif loc_0 == By.PARTIAL_LINK_TEXT: loc_str = f'//a[contains(text(),"{loc[1]}")]' else: