完善按属性定位元素的语法

This commit is contained in:
g1879 2021-12-09 10:04:36 +08:00
parent 9c9c060149
commit 7207f8606a

View File

@ -161,21 +161,28 @@ def _make_single_xpath_str(tag: str, text: str) -> str:
""" """
arg_list = [] if tag == '*' else [f'name()="{tag}"'] arg_list = [] if tag == '*' else [f'name()="{tag}"']
arg_str = txt_str = '' arg_str = txt_str = ''
r = split(r'([:=])', text, maxsplit=1)
if len(r) != 3 and len(r[0]) > 1: if text == '@':
arg_str = 'normalize-space(text())' if r[0] in ('@text()', '@tx()') else f'{r[0]}' arg_str = 'not(@*)'
elif len(r) == 3 and len(r[0]) > 1: else:
if r[1] == '=': # 精确查找 r = split(r'([:=])', text, maxsplit=1)
arg = '.' if r[0] in ('@text()', '@tx()') else r[0] len_r = len(r)
arg_str = f'{arg}={_make_search_str(r[2])}' len_r0 = len(r[0])
else: # 模糊查找 if len_r != 3 and len_r0 > 1:
if r[0] in ('@text()', '@tx()'): arg_str = 'normalize-space(text())' if r[0] in ('@text()', '@tx()') else f'{r[0]}'
txt_str = f'/text()[contains(., {_make_search_str(r[2])})]/..'
arg_str = '' elif len_r == 3 and len_r0 > 1:
else: if r[1] == '=': # 精确查找
arg_str = f"contains({r[0]},{_make_search_str(r[2])})" arg = '.' if r[0] in ('@text()', '@tx()') else r[0]
arg_str = f'{arg}={_make_search_str(r[2])}'
else: # 模糊查找
if r[0] in ('@text()', '@tx()'):
txt_str = f'/text()[contains(., {_make_search_str(r[2])})]/..'
arg_str = ''
else:
arg_str = f"contains({r[0]},{_make_search_str(r[2])})"
if arg_str: if arg_str:
arg_list.append(arg_str) arg_list.append(arg_str)
@ -195,11 +202,15 @@ def _make_multi_xpath_str(tag: str, text: str) -> str:
for arg in args[1:]: for arg in args[1:]:
r = split(r'([:=])', arg, maxsplit=1) r = split(r'([:=])', arg, maxsplit=1)
arg_str = '' arg_str = ''
len_r = len(r)
if len(r) != 3 and r[0]: if not r[0]:
arg_str = 'not(@*)'
elif len_r != 3:
arg_str = 'normalize-space(text())' if r[0] in ('text()', 'tx()') else f'@{r[0]}' arg_str = 'normalize-space(text())' if r[0] in ('text()', 'tx()') else f'@{r[0]}'
elif len(r) == 3: elif len_r == 3:
arg = '.' if r[0] in ('text()', 'tx()') else f'@{r[0]}' arg = '.' if r[0] in ('text()', 'tx()') else f'@{r[0]}'
if r[1] == '=': if r[1] == '=':
arg_str = f'{arg}={_make_search_str(r[2])}' arg_str = f'{arg}={_make_search_str(r[2])}'