mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
调整格式
This commit is contained in:
parent
c44705c3ee
commit
0a3cdaf8f3
@ -93,14 +93,18 @@ def get_loc_from_str(loc: str) -> tuple:
|
|||||||
css:div.ele_class \n
|
css:div.ele_class \n
|
||||||
"""
|
"""
|
||||||
loc_by = 'xpath'
|
loc_by = 'xpath'
|
||||||
if loc.startswith('@'): # 根据属性查找
|
|
||||||
|
# 根据属性查找
|
||||||
|
if loc.startswith('@'):
|
||||||
r = re_SPLIT(r'([:=])', loc[1:], maxsplit=1)
|
r = re_SPLIT(r'([:=])', loc[1:], maxsplit=1)
|
||||||
if len(r) == 3:
|
if len(r) == 3:
|
||||||
mode = 'exact' if r[1] == '=' else 'fuzzy'
|
mode = 'exact' if r[1] == '=' else 'fuzzy'
|
||||||
loc_str = _make_xpath_str('*', f'@{r[0]}', r[2], mode)
|
loc_str = _make_xpath_str('*', f'@{r[0]}', r[2], mode)
|
||||||
else:
|
else:
|
||||||
loc_str = f'//*[@{loc[1:]}]'
|
loc_str = f'//*[@{loc[1:]}]'
|
||||||
elif loc.startswith(('tag=', 'tag:')): # 根据tag name查找
|
|
||||||
|
# 根据tag name查找
|
||||||
|
elif loc.startswith(('tag=', 'tag:')):
|
||||||
if '@' not in loc[4:]:
|
if '@' not in loc[4:]:
|
||||||
loc_str = f'//*[name()="{loc[4:]}"]'
|
loc_str = f'//*[name()="{loc[4:]}"]'
|
||||||
else:
|
else:
|
||||||
@ -112,22 +116,31 @@ def get_loc_from_str(loc: str) -> tuple:
|
|||||||
loc_str = _make_xpath_str(at_lst[0], arg_str, r[2], mode)
|
loc_str = _make_xpath_str(at_lst[0], arg_str, r[2], mode)
|
||||||
else:
|
else:
|
||||||
loc_str = f'//*[name()="{at_lst[0]}" and @{r[0]}]'
|
loc_str = f'//*[name()="{at_lst[0]}" and @{r[0]}]'
|
||||||
elif loc.startswith(('text=', 'text:')): # 根据文本查找
|
|
||||||
|
# 根据文本查找
|
||||||
|
elif loc.startswith(('text=', 'text:')):
|
||||||
if len(loc) > 5:
|
if len(loc) > 5:
|
||||||
mode = 'exact' if loc[4] == '=' else 'fuzzy'
|
mode = 'exact' if loc[4] == '=' else 'fuzzy'
|
||||||
loc_str = _make_xpath_str('*', 'text()', loc[5:], mode)
|
loc_str = _make_xpath_str('*', 'text()', loc[5:], mode)
|
||||||
else:
|
else:
|
||||||
loc_str = '//*[not(text())]'
|
loc_str = '//*[not(text())]'
|
||||||
elif loc.startswith(('xpath=', 'xpath:')): # 用xpath查找
|
|
||||||
|
# 用xpath查找
|
||||||
|
elif loc.startswith(('xpath=', 'xpath:')):
|
||||||
loc_str = loc[6:]
|
loc_str = loc[6:]
|
||||||
elif loc.startswith(('css=', 'css:')): # 用css selector查找
|
|
||||||
|
# 用css selector查找
|
||||||
|
elif loc.startswith(('css=', 'css:')):
|
||||||
loc_by = 'css selector'
|
loc_by = 'css selector'
|
||||||
loc_str = loc[4:]
|
loc_str = loc[4:]
|
||||||
|
|
||||||
|
# 根据文本模糊查找
|
||||||
else:
|
else:
|
||||||
if loc:
|
if loc:
|
||||||
loc_str = _make_xpath_str('*', 'text()', loc, 'fuzzy')
|
loc_str = _make_xpath_str('*', 'text()', loc, 'fuzzy')
|
||||||
else:
|
else:
|
||||||
loc_str = '//*[not(text())]'
|
loc_str = '//*[not(text())]'
|
||||||
|
|
||||||
return loc_by, loc_str
|
return loc_by, loc_str
|
||||||
|
|
||||||
|
|
||||||
@ -140,10 +153,13 @@ def _make_xpath_str(tag: str, arg: str, val: str, mode: str = 'fuzzy') -> str:
|
|||||||
:return: xpath字符串
|
:return: xpath字符串
|
||||||
"""
|
"""
|
||||||
tag_name = '' if tag == '*' else f'name()="{tag}" and '
|
tag_name = '' if tag == '*' else f'name()="{tag}" and '
|
||||||
|
|
||||||
if mode == 'exact':
|
if mode == 'exact':
|
||||||
return f'//*[{tag_name}{arg}={_make_search_str(val)}]'
|
return f'//*[{tag_name}{arg}={_make_search_str(val)}]'
|
||||||
|
|
||||||
elif mode == 'fuzzy':
|
elif mode == 'fuzzy':
|
||||||
return f"//*[{tag_name}contains({arg},{_make_search_str(val)})]"
|
return f"//*[{tag_name}contains({arg},{_make_search_str(val)})]"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Argument mode can only be 'exact' or 'fuzzy'.")
|
raise ValueError("Argument mode can only be 'exact' or 'fuzzy'.")
|
||||||
|
|
||||||
@ -156,10 +172,13 @@ def _make_search_str(search_str: str) -> str:
|
|||||||
parts = search_str.split('"')
|
parts = search_str.split('"')
|
||||||
parts_num = len(parts)
|
parts_num = len(parts)
|
||||||
search_str = 'concat('
|
search_str = 'concat('
|
||||||
|
|
||||||
for key, i in enumerate(parts):
|
for key, i in enumerate(parts):
|
||||||
search_str += f'"{i}"'
|
search_str += f'"{i}"'
|
||||||
search_str += ',' + '\'"\',' if key < parts_num - 1 else ''
|
search_str += ',' + '\'"\',' if key < parts_num - 1 else ''
|
||||||
|
|
||||||
search_str += ',"")'
|
search_str += ',"")'
|
||||||
|
|
||||||
return search_str
|
return search_str
|
||||||
|
|
||||||
|
|
||||||
@ -170,23 +189,32 @@ def translate_loc_to_xpath(loc: tuple) -> tuple:
|
|||||||
"""
|
"""
|
||||||
loc_by = 'xpath'
|
loc_by = 'xpath'
|
||||||
loc_str = None
|
loc_str = None
|
||||||
|
|
||||||
if loc[0] == 'xpath':
|
if loc[0] == 'xpath':
|
||||||
loc_str = loc[1]
|
loc_str = loc[1]
|
||||||
|
|
||||||
elif loc[0] == 'css selector':
|
elif loc[0] == 'css selector':
|
||||||
loc_by = 'css selector'
|
loc_by = 'css selector'
|
||||||
loc_str = loc[1]
|
loc_str = loc[1]
|
||||||
|
|
||||||
elif loc[0] == 'id':
|
elif loc[0] == 'id':
|
||||||
loc_str = f'//*[@id="{loc[1]}"]'
|
loc_str = f'//*[@id="{loc[1]}"]'
|
||||||
|
|
||||||
elif loc[0] == 'class name':
|
elif loc[0] == 'class name':
|
||||||
loc_str = f'//*[@class="{loc[1]}"]'
|
loc_str = f'//*[@class="{loc[1]}"]'
|
||||||
|
|
||||||
elif loc[0] == 'link text':
|
elif loc[0] == 'link text':
|
||||||
loc_str = f'//a[text()="{loc[1]}"]'
|
loc_str = f'//a[text()="{loc[1]}"]'
|
||||||
|
|
||||||
elif loc[0] == 'name':
|
elif loc[0] == 'name':
|
||||||
loc_str = f'//*[@name="{loc[1]}"]'
|
loc_str = f'//*[@name="{loc[1]}"]'
|
||||||
|
|
||||||
elif loc[0] == 'tag name':
|
elif loc[0] == 'tag name':
|
||||||
loc_str = f'//{loc[1]}'
|
loc_str = f'//{loc[1]}'
|
||||||
|
|
||||||
elif loc[0] == 'partial link text':
|
elif loc[0] == 'partial link text':
|
||||||
loc_str = f'//a[contains(text(),"{loc[1]}")]'
|
loc_str = f'//a[contains(text(),"{loc[1]}")]'
|
||||||
|
|
||||||
return loc_by, loc_str
|
return loc_by, loc_str
|
||||||
|
|
||||||
|
|
||||||
@ -198,16 +226,20 @@ def get_available_file_name(folder_path: str, file_name: str) -> str:
|
|||||||
"""
|
"""
|
||||||
folder_path = Path(folder_path).absolute()
|
folder_path = Path(folder_path).absolute()
|
||||||
file_Path = folder_path.joinpath(file_name)
|
file_Path = folder_path.joinpath(file_name)
|
||||||
|
|
||||||
while file_Path.exists():
|
while file_Path.exists():
|
||||||
ext_name = file_Path.suffix
|
ext_name = file_Path.suffix
|
||||||
base_name = file_Path.stem
|
base_name = file_Path.stem
|
||||||
num = base_name.split(' ')[-1]
|
num = base_name.split(' ')[-1]
|
||||||
|
|
||||||
if num[0] == '(' and num[-1] == ')' and num[1:-1].isdigit():
|
if num[0] == '(' and num[-1] == ')' and num[1:-1].isdigit():
|
||||||
num = int(num[1:-1])
|
num = int(num[1:-1])
|
||||||
file_name = f'{base_name.replace(f"({num})", "", -1)}({num + 1}){ext_name}'
|
file_name = f'{base_name.replace(f"({num})", "", -1)}({num + 1}){ext_name}'
|
||||||
else:
|
else:
|
||||||
file_name = f'{base_name} (1){ext_name}'
|
file_name = f'{base_name} (1){ext_name}'
|
||||||
|
|
||||||
file_Path = folder_path.joinpath(file_name)
|
file_Path = folder_path.joinpath(file_name)
|
||||||
|
|
||||||
return file_name
|
return file_name
|
||||||
|
|
||||||
|
|
||||||
@ -219,6 +251,7 @@ def clean_folder(folder_path: str, ignore: list = None) -> None:
|
|||||||
"""
|
"""
|
||||||
ignore = [] if not ignore else ignore
|
ignore = [] if not ignore else ignore
|
||||||
p = Path(folder_path)
|
p = Path(folder_path)
|
||||||
|
|
||||||
for f in p.iterdir():
|
for f in p.iterdir():
|
||||||
if f.name not in ignore:
|
if f.name not in ignore:
|
||||||
if f.is_file():
|
if f.is_file():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user