mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
4.1.0.7修复xpath不唯一的问题
This commit is contained in:
parent
bd79de1abd
commit
422c93d8b6
@ -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'
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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路径"""
|
||||
...
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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路径
|
||||
"""
|
||||
...
|
||||
|
Loading…
x
Reference in New Issue
Block a user