4.1.0.7修复xpath不唯一的问题

This commit is contained in:
g1879 2024-10-07 23:02:19 +08:00
parent bd79de1abd
commit 422c93d8b6
7 changed files with 20 additions and 26 deletions

View File

@ -12,4 +12,4 @@ from ._pages.chromium_page import ChromiumPage
from ._pages.session_page import SessionPage from ._pages.session_page import SessionPage
from ._pages.web_page import WebPage from ._pages.web_page import WebPage
__version__ = '4.1.0.6' __version__ = '4.1.0.7'

View File

@ -124,11 +124,11 @@ class DrissionElement(BaseElement):
@property @property
def css_path(self): def css_path(self):
return self._get_ele_path('css') return self._get_ele_path(xpath=False)
@property @property
def xpath(self): def xpath(self):
return self._get_ele_path('xpath') return self._get_ele_path()
@property @property
def comments(self): def comments(self):
@ -255,7 +255,7 @@ class DrissionElement(BaseElement):
def attr(self, name: str): def attr(self, name: str):
return '' return ''
def _get_ele_path(self, mode): def _get_ele_path(self, xpath=True):
return '' return ''
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None): def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):

View File

@ -352,7 +352,7 @@ class DrissionElement(BaseElement):
@abstractmethod @abstractmethod
def attr(self, name: str) -> str: ... 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): class BasePage(BaseParser):

View File

@ -636,16 +636,14 @@ class ChromiumElement(DrissionElement):
self._obj_id = self._get_obj_id(backend_id=self._backend_id) self._obj_id = self._get_obj_id(backend_id=self._backend_id)
self._node_id = self._get_node_id(obj_id=self._obj_id) self._node_id = self._get_node_id(obj_id=self._obj_id)
def _get_ele_path(self, mode): def _get_ele_path(self, xpath=True):
if mode == 'xpath': if xpath:
txt1 = 'let tag = el.nodeName.toLowerCase();' txt1 = 'let tag = el.nodeName.toLowerCase();'
txt3 = ''' && sib.nodeName.toLowerCase()==tag''' txt3 = ''' && sib.nodeName.toLowerCase()===tag'''
txt4 = ''' txt4 = '''path = '/' + tag + '[' + nth + ']' + path;'''
if(nth>1){path = '/' + tag + '[' + nth + ']' + path;}
else{path = '/' + tag + path;}'''
txt5 = '''return path;''' txt5 = '''return path;'''
elif mode == 'css': else:
txt1 = ''' txt1 = '''
let i = el.getAttribute("id"); let i = el.getAttribute("id");
if (i){path = '>' + el.tagName.toLowerCase() + "#" + i + path; if (i){path = '>' + el.tagName.toLowerCase() + "#" + i + path;
@ -655,9 +653,6 @@ class ChromiumElement(DrissionElement):
txt4 = '''path = '>' + el.tagName.toLowerCase() + ":nth-child(" + nth + ")" + path;''' txt4 = '''path = '>' + el.tagName.toLowerCase() + ":nth-child(" + nth + ")" + path;'''
txt5 = '''return path.substr(1);''' txt5 = '''return path.substr(1);'''
else:
raise ValueError(f"mode参数只能是'xpath''css',现在是:'{mode}'")
js = '''function(){ js = '''function(){
function e(el) { function e(el) {
//return el; //return el;
@ -677,8 +672,7 @@ class ChromiumElement(DrissionElement):
} }
return e(this);} return e(this);}
''' '''
t = self._run_js(js) return self._run_js(js)
return f'{t}' if mode == 'css' else t
def _set_file_input(self, files): def _set_file_input(self, files):
if isinstance(files, str): if isinstance(files, str):

View File

@ -587,7 +587,7 @@ class ChromiumElement(DrissionElement):
"""根据backend id刷新其它id""" """根据backend id刷新其它id"""
... ...
def _get_ele_path(self, mode: str) -> str: def _get_ele_path(self, xpath: bool = True) -> str:
"""返获取绝对的css路径或xpath路径""" """返获取绝对的css路径或xpath路径"""
... ...

View File

@ -145,25 +145,25 @@ class SessionElement(DrissionElement):
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None): def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
return make_session_ele(self, locator, index=index) return make_session_ele(self, locator, index=index)
def _get_ele_path(self, mode): def _get_ele_path(self, xpath=True):
path_str = '' path_str = ''
ele = self ele = self
while ele: 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') id_ = ele.attr('id')
if id_: if id_:
path_str = f'>{ele.tag}#{id_}{path_str}' path_str = f'>{ele.tag}#{id_}{path_str}'
break break
brothers = len(ele.eles(f'xpath:./preceding-sibling::*')) brothers = len(ele.eles(f'xpath:./preceding-sibling::*'))
path_str = f'>{ele.tag}:nth-child({brothers + 1}){path_str}' 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() 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): def make_session_ele(html_or_ele, loc=None, index=1, method=None):

View File

@ -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路径 """获取css路径或xpath路径
:param mode: 'css' 'xpath' :param xpath: 用xpath还是css
:return: css路径或xpath路径 :return: css路径或xpath路径
""" """
... ...