mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
DriverElement和SessionElement增加link属性
This commit is contained in:
parent
eb866ba29e
commit
70cc8da88d
@ -32,6 +32,13 @@ class DriverElement(DrissionElement):
|
||||
loc_or_str: Union[Tuple[str, str], str],
|
||||
mode: str = 'single',
|
||||
timeout: float = None):
|
||||
"""实现查找元素的简化写法 \n
|
||||
例:ele2 = ele1('@id=ele_id') \n
|
||||
:param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param mode: 'single' 或 'all',对应查找一个或全部
|
||||
:param timeout: 超时时间
|
||||
:return: DriverElement对象
|
||||
"""
|
||||
return self.ele(loc_or_str, mode, timeout or self.timeout)
|
||||
|
||||
# -----------------共有属性-------------------
|
||||
@ -74,6 +81,11 @@ class DriverElement(DrissionElement):
|
||||
"""返回元素内所有文本"""
|
||||
return self.attr('innerText')
|
||||
|
||||
@property
|
||||
def link(self) -> str:
|
||||
"""返回href或src绝对url"""
|
||||
return self.attr('href') or self.attr('src')
|
||||
|
||||
@property
|
||||
def css_path(self) -> str:
|
||||
"""返回当前元素的css路径"""
|
||||
@ -212,15 +224,6 @@ class DriverElement(DrissionElement):
|
||||
raise ValueError('Argument loc_or_str can only be tuple or str.')
|
||||
|
||||
loc_str = loc_or_str[1]
|
||||
# if loc_or_str[0] == 'xpath':
|
||||
# # 处理语句最前面的(
|
||||
# brackets = len(re.match(r'\(*', loc_or_str[1]).group(0))
|
||||
# bracket, loc_str = '(' * brackets, loc_or_str[1][brackets:]
|
||||
#
|
||||
# # 确保查询语句最前面是.
|
||||
# loc_str = loc_str if loc_str.startswith(('.', '/')) else f'.//{loc_str}'
|
||||
# loc_str = loc_str if loc_str.startswith('.') else f'.{loc_str}'
|
||||
# loc_str = f'{bracket}{loc_str}'
|
||||
|
||||
if loc_or_str[0] == 'xpath' and loc_or_str[1].lstrip().startswith('/'):
|
||||
loc_str = f'.{loc_str}'
|
||||
|
@ -26,6 +26,12 @@ class SessionElement(DrissionElement):
|
||||
return f'<SessionElement {self.tag} {" ".join(attrs)}>'
|
||||
|
||||
def __call__(self, loc_or_str: Union[Tuple[str, str], str], mode: str = 'single'):
|
||||
"""实现查找元素的简化写法 \n
|
||||
例:ele2 = ele1('@id=ele_id') \n
|
||||
:param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param mode: 'single' 或 'all',对应查找一个或全部
|
||||
:return: SessionElement对象
|
||||
"""
|
||||
return self.ele(loc_or_str, mode)
|
||||
|
||||
@property
|
||||
@ -53,7 +59,12 @@ class SessionElement(DrissionElement):
|
||||
@property
|
||||
def text(self) -> str:
|
||||
"""返回元素内所有文本"""
|
||||
return self._inner_ele.text_content()
|
||||
return str(self._inner_ele.text_content())
|
||||
|
||||
@property
|
||||
def link(self) -> str:
|
||||
"""返回href或src绝对url"""
|
||||
return self.attr('href') or self.attr('src')
|
||||
|
||||
@property
|
||||
def css_path(self) -> str:
|
||||
@ -133,8 +144,8 @@ class SessionElement(DrissionElement):
|
||||
if attr == 'href':
|
||||
link = self.inner_ele.get('href')
|
||||
|
||||
# 若链接为js或邮件,直接返回
|
||||
if link.lower().startswith(('javascript:', 'mailto:')):
|
||||
# 若为链接为None、js或邮件,直接返回
|
||||
if not link or link.lower().startswith(('javascript:', 'mailto:')):
|
||||
return link
|
||||
|
||||
# 其它情况直接返回绝对url
|
||||
@ -193,12 +204,6 @@ class SessionElement(DrissionElement):
|
||||
|
||||
element = self
|
||||
loc_str = loc_or_str[1]
|
||||
# if loc_or_str[0] == 'xpath':
|
||||
# brackets = len(re.match(r'\(*', loc_or_str[1]).group(0))
|
||||
# bracket, loc_str = '(' * brackets, loc_or_str[1][brackets:]
|
||||
# loc_str = loc_str if loc_str.startswith(('.', '/')) else f'.//{loc_str}'
|
||||
# loc_str = loc_str if loc_str.startswith('.') else f'.{loc_str}'
|
||||
# loc_str = f'{bracket}{loc_str}'
|
||||
|
||||
if loc_or_str[0] == 'xpath' and loc_or_str[1].lstrip().startswith('/'):
|
||||
loc_str = f'.{loc_str}'
|
||||
@ -239,8 +244,14 @@ class SessionElement(DrissionElement):
|
||||
return self.ele(loc_or_str, mode='all')
|
||||
|
||||
# -----------------私有函数-------------------
|
||||
def _make_absolute(self, link):
|
||||
"""生成绝对url"""
|
||||
def _make_absolute(self, link) -> str:
|
||||
"""获取绝对url
|
||||
:param link: 超链接
|
||||
:return: 绝对链接
|
||||
"""
|
||||
if not link:
|
||||
return link
|
||||
|
||||
parsed = urlparse(link)._asdict()
|
||||
|
||||
# 相对路径,与页面url拼接并返回
|
||||
@ -257,7 +268,10 @@ class SessionElement(DrissionElement):
|
||||
return link
|
||||
|
||||
def _get_ele_path(self, mode) -> str:
|
||||
"""获取css路径或xpath路径"""
|
||||
"""获取css路径或xpath路径
|
||||
:param mode: 'css' 或 'xpath'
|
||||
:return: css路径或xpath路径
|
||||
"""
|
||||
path_str = ''
|
||||
ele = self
|
||||
|
||||
|
59
README.en.md
59
README.en.md
@ -566,16 +566,17 @@ ele2 = ele1('tag:li')
|
||||
## Get element attributes
|
||||
|
||||
```python
|
||||
element.html # return element outerHTML
|
||||
element.html # Return element outerHTML
|
||||
element.inner_html # Return element innerHTML
|
||||
element.tag # return element tag name
|
||||
element.text # return element innerText value
|
||||
element.tag # Return element tag name
|
||||
element.text # Return element innerText value
|
||||
element.link # Returns absolute href or src value of the element.
|
||||
element.texts() # Returns the text of all direct child nodes in the element, including elements and text nodes, you can specify to return only text nodes
|
||||
element.attrs # Return a dictionary of all attributes of the element
|
||||
element.attr(attr) # Return the value of the specified attribute of the element
|
||||
element.css_path # Return the absolute css path of the element
|
||||
element.xpath # Return the absolute xpath path of the element
|
||||
element.parent # return element parent element
|
||||
element.parent # Return element parent element
|
||||
element.next # Return the next sibling element of the element
|
||||
element.prev # Return the previous sibling element of the element
|
||||
element.parents(num) # Return the numth parent element
|
||||
@ -584,7 +585,7 @@ element.prevs(num, mode) # Return the first few elements or nodes
|
||||
element.ele(loc_or_str, timeout) # Return the first sub- element, attribute or node text of the current element that meets the conditions
|
||||
element.eles(loc_or_str, timeout) # Return all eligible sub- elements, attributes or node texts of the current element
|
||||
|
||||
# Driver mode unique:
|
||||
# d mode unique:
|
||||
element.before # Get pseudo element before content
|
||||
element.after # Get pseudo element after content
|
||||
element.is_valid # Used to determine whether the element is still in dom
|
||||
@ -1611,6 +1612,14 @@ Returns: str
|
||||
|
||||
|
||||
|
||||
### link
|
||||
|
||||
Returns absolute href or src value of the element.
|
||||
|
||||
Returns: str
|
||||
|
||||
|
||||
|
||||
### css_path
|
||||
|
||||
Returns the absolute path of the element css selector.
|
||||
@ -2003,22 +2012,6 @@ Returns: HtmlElement
|
||||
|
||||
|
||||
|
||||
### attrs
|
||||
|
||||
Returns the names and values of all attributes of the element in dictionary format.
|
||||
|
||||
Returns: dict
|
||||
|
||||
|
||||
|
||||
### text
|
||||
|
||||
Returns the text within the element, namely innerText.
|
||||
|
||||
Returns: str
|
||||
|
||||
|
||||
|
||||
### html
|
||||
|
||||
Returns the outerHTML text of the element.
|
||||
@ -2043,6 +2036,30 @@ Returns: srt
|
||||
|
||||
|
||||
|
||||
### attrs
|
||||
|
||||
Returns the names and values of all attributes of the element in dictionary format.
|
||||
|
||||
Returns: dict
|
||||
|
||||
|
||||
|
||||
### text
|
||||
|
||||
Returns the text within the element, namely innerText.
|
||||
|
||||
Returns: str
|
||||
|
||||
|
||||
|
||||
### link
|
||||
|
||||
Returns absolute href or src value of the element.
|
||||
|
||||
Returns: str
|
||||
|
||||
|
||||
|
||||
### css_path
|
||||
|
||||
Returns the absolute path of the element css selector.
|
||||
|
@ -570,6 +570,7 @@ element.html # 返回元素outerHTML
|
||||
element.inner_html # 返回元素innerHTML
|
||||
element.tag # 返回元素tag name
|
||||
element.text # 返回元素innerText值
|
||||
element.link # 返回元素href或src绝对url
|
||||
element.texts() # 返回元素内所有直接子节点的文本,包括元素和文本节点,可指定只返回文本节点
|
||||
element.attrs # 返回元素所有属性的字典
|
||||
element.attr(attr) # 返回元素指定属性的值
|
||||
@ -584,7 +585,7 @@ element.prevs(num, mode) # 返回前面第几个元素或节点
|
||||
element.ele(loc_or_str, timeout) # 返回当前元素下级第一个符合条件的子元素、属性或节点文本
|
||||
element.eles(loc_or_str, timeout) # 返回当前元素下级所有符合条件的子元素、属性或节点文本
|
||||
|
||||
# driver模式独有:
|
||||
# d 模式独有:
|
||||
element.before # 获取伪元素before内容
|
||||
element.after # 获取伪元素after内容
|
||||
element.is_valid # 用于判断元素是否还在dom中
|
||||
@ -1611,6 +1612,14 @@ driver模式的元素对象,包装了一个WebElement对象,并封装了常
|
||||
|
||||
|
||||
|
||||
### link
|
||||
|
||||
返回元素href或src绝对url。
|
||||
|
||||
返回: str
|
||||
|
||||
|
||||
|
||||
### css_path
|
||||
|
||||
返回元素css selector绝对路径。
|
||||
@ -2003,22 +2012,6 @@ session模式的元素对象,包装了一个Element对象,并封装了常用
|
||||
|
||||
|
||||
|
||||
### attrs
|
||||
|
||||
以字典格式返回元素所有属性的名称和值。
|
||||
|
||||
返回: dict
|
||||
|
||||
|
||||
|
||||
### text
|
||||
|
||||
返回元素内的文本,即innerText。
|
||||
|
||||
返回: str
|
||||
|
||||
|
||||
|
||||
### html
|
||||
|
||||
返回元素outerHTML文本。
|
||||
@ -2043,6 +2036,30 @@ session模式的元素对象,包装了一个Element对象,并封装了常用
|
||||
|
||||
|
||||
|
||||
### attrs
|
||||
|
||||
以字典格式返回元素所有属性的名称和值。
|
||||
|
||||
返回: dict
|
||||
|
||||
|
||||
|
||||
### text
|
||||
|
||||
返回元素内的文本,即innerText。
|
||||
|
||||
返回: str
|
||||
|
||||
|
||||
|
||||
### link
|
||||
|
||||
返回元素href或src绝对url。
|
||||
|
||||
返回: str
|
||||
|
||||
|
||||
|
||||
### css_path
|
||||
|
||||
返回元素css selector绝对路径。
|
||||
|
Loading…
x
Reference in New Issue
Block a user