From 422c93d8b6dac8b773940b3b98679baeda2fc9f3 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 7 Oct 2024 23:02:19 +0800 Subject: [PATCH] =?UTF-8?q?4.1.0.7=E4=BF=AE=E5=A4=8Dxpath=E4=B8=8D?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_base/base.py | 6 +++--- DrissionPage/_base/base.pyi | 2 +- DrissionPage/_elements/chromium_element.py | 18 ++++++------------ DrissionPage/_elements/chromium_element.pyi | 2 +- DrissionPage/_elements/session_element.py | 12 ++++++------ DrissionPage/_elements/session_element.pyi | 4 ++-- 7 files changed, 20 insertions(+), 26 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index c1b2f89..65d5714 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -12,4 +12,4 @@ from ._pages.chromium_page import ChromiumPage from ._pages.session_page import SessionPage from ._pages.web_page import WebPage -__version__ = '4.1.0.6' +__version__ = '4.1.0.7' diff --git a/DrissionPage/_base/base.py b/DrissionPage/_base/base.py index c4ca655..8132f4c 100644 --- a/DrissionPage/_base/base.py +++ b/DrissionPage/_base/base.py @@ -124,11 +124,11 @@ class DrissionElement(BaseElement): @property def css_path(self): - return self._get_ele_path('css') + return self._get_ele_path(xpath=False) @property def xpath(self): - return self._get_ele_path('xpath') + return self._get_ele_path() @property def comments(self): @@ -255,7 +255,7 @@ class DrissionElement(BaseElement): def attr(self, name: str): return '' - def _get_ele_path(self, mode): + def _get_ele_path(self, xpath=True): return '' def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None): diff --git a/DrissionPage/_base/base.pyi b/DrissionPage/_base/base.pyi index 20fef8e..7112b31 100644 --- a/DrissionPage/_base/base.pyi +++ b/DrissionPage/_base/base.pyi @@ -352,7 +352,7 @@ class DrissionElement(BaseElement): @abstractmethod def attr(self, name: str) -> str: ... - def _get_ele_path(self, mode) -> str: ... + def _get_ele_path(self, xpath: bool = True) -> str: ... class BasePage(BaseParser): diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index c335efd..9424e2f 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -636,16 +636,14 @@ class ChromiumElement(DrissionElement): self._obj_id = self._get_obj_id(backend_id=self._backend_id) self._node_id = self._get_node_id(obj_id=self._obj_id) - def _get_ele_path(self, mode): - if mode == 'xpath': + def _get_ele_path(self, xpath=True): + if xpath: txt1 = 'let tag = el.nodeName.toLowerCase();' - txt3 = ''' && sib.nodeName.toLowerCase()==tag''' - txt4 = ''' - if(nth>1){path = '/' + tag + '[' + nth + ']' + path;} - else{path = '/' + tag + path;}''' + txt3 = ''' && sib.nodeName.toLowerCase()===tag''' + txt4 = '''path = '/' + tag + '[' + nth + ']' + path;''' txt5 = '''return path;''' - elif mode == 'css': + else: txt1 = ''' let i = el.getAttribute("id"); if (i){path = '>' + el.tagName.toLowerCase() + "#" + i + path; @@ -655,9 +653,6 @@ class ChromiumElement(DrissionElement): txt4 = '''path = '>' + el.tagName.toLowerCase() + ":nth-child(" + nth + ")" + path;''' txt5 = '''return path.substr(1);''' - else: - raise ValueError(f"mode参数只能是'xpath'或'css',现在是:'{mode}'。") - js = '''function(){ function e(el) { //return el; @@ -677,8 +672,7 @@ class ChromiumElement(DrissionElement): } return e(this);} ''' - t = self._run_js(js) - return f'{t}' if mode == 'css' else t + return self._run_js(js) def _set_file_input(self, files): if isinstance(files, str): diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index 09ab107..d87f54b 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -587,7 +587,7 @@ class ChromiumElement(DrissionElement): """根据backend id刷新其它id""" ... - def _get_ele_path(self, mode: str) -> str: + def _get_ele_path(self, xpath: bool = True) -> str: """返获取绝对的css路径或xpath路径""" ... diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index d0cba7a..914f7dc 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -145,25 +145,25 @@ class SessionElement(DrissionElement): def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None): return make_session_ele(self, locator, index=index) - def _get_ele_path(self, mode): + def _get_ele_path(self, xpath=True): path_str = '' ele = self while ele: - if mode == 'css': + if xpath: + brothers = len(ele.eles(f'xpath:./preceding-sibling::{ele.tag}')) + path_str = f'/{ele.tag}[{brothers + 1}]{path_str}' + else: id_ = ele.attr('id') if id_: path_str = f'>{ele.tag}#{id_}{path_str}' break brothers = len(ele.eles(f'xpath:./preceding-sibling::*')) path_str = f'>{ele.tag}:nth-child({brothers + 1}){path_str}' - else: - brothers = len(ele.eles(f'xpath:./preceding-sibling::{ele.tag}')) - path_str = f'/{ele.tag}[{brothers + 1}]{path_str}' if brothers > 0 else f'/{ele.tag}{path_str}' ele = ele.parent() - return f'{path_str[1:]}' if mode == 'css' else path_str + return path_str if xpath else f'{path_str[1:]}' def make_session_ele(html_or_ele, loc=None, index=1, method=None): diff --git a/DrissionPage/_elements/session_element.pyi b/DrissionPage/_elements/session_element.pyi index b883540..eac4d36 100644 --- a/DrissionPage/_elements/session_element.pyi +++ b/DrissionPage/_elements/session_element.pyi @@ -283,9 +283,9 @@ class SessionElement(DrissionElement): """ ... - def _get_ele_path(self, mode: str) -> str: + def _get_ele_path(self, xpath: bool=True) -> str: """获取css路径或xpath路径 - :param mode: 'css' 或 'xpath' + :param xpath: 用xpath还是css :return: css路径或xpath路径 """ ...