mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
DrissinElement增加xpath属性,未完成
This commit is contained in:
parent
b065e7994f
commit
e0d4b37655
@ -52,12 +52,29 @@ class DrissionElement(object):
|
|||||||
def prev(self):
|
def prev(self):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@property
|
||||||
|
def xpath(self):
|
||||||
|
def get_xpath(ele: DrissionElement, xpath_str=''):
|
||||||
|
ele_id = ele.attr('id')
|
||||||
|
if ele_id:
|
||||||
|
return None, f'//*[@id="{ele_id}"]{xpath_str}'
|
||||||
|
else:
|
||||||
|
brothers = len(ele.eles(f'xpath:./preceding-sibling::{ele.tag}', timeout=0.001)) # FIXME: 修改这里
|
||||||
|
xpath_str = f'/{ele.tag}[{brothers + 1}]{xpath_str}' if brothers > 0 else f'/{ele.tag}{xpath_str}'
|
||||||
|
ele = ele.parent
|
||||||
|
print(xpath_str)
|
||||||
|
while ele:
|
||||||
|
ele, xpath_str = get_xpath(ele, xpath_str)
|
||||||
|
return ele, xpath_str
|
||||||
|
|
||||||
|
return get_xpath(self)[1]
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def ele(self, loc: tuple, mode: str = None, show_errmsg: bool = True):
|
def ele(self, loc: Union[tuple, str], mode: str = None, show_errmsg: bool = True):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def eles(self, loc: tuple, show_errmsg: bool = True):
|
def eles(self, loc: Union[tuple, str], show_errmsg: bool = True):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -92,7 +92,7 @@ class DriverElement(DrissionElement):
|
|||||||
:return: DriverElement对象
|
:return: DriverElement对象
|
||||||
"""
|
"""
|
||||||
loc = 'xpath', f'.{"/.." * num}'
|
loc = 'xpath', f'.{"/.." * num}'
|
||||||
return self.ele(loc, timeout=1, show_errmsg=False)
|
return self.ele(loc, timeout=0.01, show_errmsg=False)
|
||||||
|
|
||||||
def nexts(self, num: int = 1):
|
def nexts(self, num: int = 1):
|
||||||
"""返回后面第num个兄弟元素 \n
|
"""返回后面第num个兄弟元素 \n
|
||||||
@ -100,7 +100,7 @@ class DriverElement(DrissionElement):
|
|||||||
:return: DriverElement对象
|
:return: DriverElement对象
|
||||||
"""
|
"""
|
||||||
loc = 'xpath', f'./following-sibling::*[{num}]'
|
loc = 'xpath', f'./following-sibling::*[{num}]'
|
||||||
return self.ele(loc, timeout=1, show_errmsg=False)
|
return self.ele(loc, timeout=0.1, show_errmsg=False)
|
||||||
|
|
||||||
def prevs(self, num: int = 1):
|
def prevs(self, num: int = 1):
|
||||||
"""返回前面第num个兄弟元素 \n
|
"""返回前面第num个兄弟元素 \n
|
||||||
@ -108,7 +108,7 @@ class DriverElement(DrissionElement):
|
|||||||
:return: DriverElement对象
|
:return: DriverElement对象
|
||||||
"""
|
"""
|
||||||
loc = 'xpath', f'./preceding-sibling::*[{num}]'
|
loc = 'xpath', f'./preceding-sibling::*[{num}]'
|
||||||
return self.ele(loc, timeout=1, show_errmsg=False)
|
return self.ele(loc, timeout=0.01, show_errmsg=False)
|
||||||
|
|
||||||
def attr(self, attr: str) -> str:
|
def attr(self, attr: str) -> str:
|
||||||
"""获取属性值 \n
|
"""获取属性值 \n
|
||||||
|
Loading…
x
Reference in New Issue
Block a user