用xpath查找元素时自动补全//

This commit is contained in:
g1879 2020-08-19 10:49:42 +08:00
parent 5f83e93a52
commit e66f6adc79
4 changed files with 13 additions and 4 deletions

View File

@ -213,7 +213,8 @@ class DriverElement(DrissionElement):
if loc_or_str[0] == 'xpath':
# 确保查询语句最前面是.
loc_str = f'.{loc_or_str[1]}' if not loc_or_str[1].startswith('.') else loc_or_str[1]
loc_str = loc_or_str[1] if loc_or_str[1].startswith(('.', '/')) else f'.//{loc_or_str[1]}'
loc_str = loc_str if loc_str.startswith('.') else f'.{loc_str}'
loc_or_str = loc_or_str[0], loc_str
else:
if loc_or_str[1].lstrip().startswith('>'):

View File

@ -112,8 +112,11 @@ class DriverPage(object):
"""
if isinstance(loc_or_ele, str):
loc_or_ele = get_loc_from_str(loc_or_ele)
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'):
loc_or_ele = 'xpath', f'//{loc_or_ele[1]}'
elif isinstance(loc_or_ele, tuple) and len(loc_or_ele) == 2:
pass
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'):
loc_or_ele = 'xpath', f'//{loc_or_ele[1]}'
elif isinstance(loc_or_ele, DriverElement):
return loc_or_ele
elif isinstance(loc_or_ele, WebElement):

View File

@ -148,9 +148,10 @@ class SessionElement(DrissionElement):
loc_str = None
if loc_or_str[0] == 'xpath':
# Element的html是包含自己的要如下处理使其只检索下级的
loc_str = loc_or_str[1] if loc_or_str[1].startswith('.') else f'.{loc_or_str[1]}'
loc_str = loc_or_str[1] if loc_or_str[1].startswith(('.', '/')) else f'.//{loc_or_str[1]}'
loc_str = loc_str if loc_str.startswith('.') else f'.{loc_str}'
elif loc_or_str[0] == 'css selector':
# Element的html是包含自己的要如下处理使其只检索下级的
loc_str = loc_or_str[1] if loc_or_str[1][0] in '>, ' else f' {loc_or_str[1]}'
loc_str = f':root>{self.tag}{loc_str}'
loc_or_str = loc_or_str[0], loc_str

View File

@ -99,8 +99,12 @@ class SessionPage(object):
"""
if isinstance(loc_or_ele, str):
loc_or_ele = get_loc_from_str(loc_or_ele)
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'):
loc_or_ele = 'xpath', f'//{loc_or_ele[1]}'
elif isinstance(loc_or_ele, tuple) and len(loc_or_ele) == 2:
loc_or_ele = translate_loc_to_xpath(loc_or_ele)
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'):
loc_or_ele = 'xpath', f'//{loc_or_ele[1]}'
elif isinstance(loc_or_ele, SessionElement):
return loc_or_ele
elif isinstance(loc_or_ele, Element):