mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
avoid_duplicate_name函数改名为get_available_file_name
This commit is contained in:
parent
1c9bc8dd24
commit
4c24a57537
@ -15,6 +15,8 @@ from selenium.webdriver.remote.webelement import WebElement
|
||||
|
||||
|
||||
class DrissionElement(object):
|
||||
"""SessionElement和DriverElement的基类"""
|
||||
|
||||
def __init__(self, ele: Union[Element, WebElement]):
|
||||
self._inner_ele = ele
|
||||
|
||||
@ -64,22 +66,22 @@ class DrissionElement(object):
|
||||
|
||||
|
||||
def get_loc_from_str(loc: str) -> tuple:
|
||||
"""处理元素查找语句
|
||||
查找方式:属性、tag name及属性、文本、xpath、css selector
|
||||
=表示精确匹配,:表示模糊匹配,无控制字符串时默认搜索该字符串
|
||||
示例:
|
||||
@class:ele_class - class含有ele_class的元素
|
||||
@class=ele_class - class等于ele_class的元素
|
||||
@class - 带class属性的元素
|
||||
tag:div - div元素
|
||||
tag:div@class:ele_class - class含有ele_class的div元素
|
||||
tag:div@class=ele_class - class等于ele_class的div元素
|
||||
tag:div@text():search_text - 文本含有search_text的div元素
|
||||
tag:div@text()=search_text - 文本等于search_text的div元素
|
||||
text:search_text - 文本含有search_text的元素
|
||||
text=search_text - 文本等于search_text的元素
|
||||
xpath://div[@class="ele_class"]
|
||||
css:div.ele_class
|
||||
"""处理元素查找语句 \n
|
||||
查找方式:属性、tag name及属性、文本、xpath、css selector \n
|
||||
=表示精确匹配,:表示模糊匹配,无控制字符串时默认搜索该字符串 \n
|
||||
示例: \n
|
||||
@class:ele_class - class含有ele_class的元素 \n
|
||||
@class=ele_class - class等于ele_class的元素 \n
|
||||
@class - 带class属性的元素 \n
|
||||
tag:div - div元素 \n
|
||||
tag:div@class:ele_class - class含有ele_class的div元素 \n
|
||||
tag:div@class=ele_class - class等于ele_class的div元素 \n
|
||||
tag:div@text():search_text - 文本含有search_text的div元素 \n
|
||||
tag:div@text()=search_text - 文本等于search_text的div元素 \n
|
||||
text:search_text - 文本含有search_text的元素 \n
|
||||
text=search_text - 文本等于search_text的元素 \n
|
||||
xpath://div[@class="ele_class"] \n
|
||||
css:div.ele_class \n
|
||||
"""
|
||||
loc_by = 'xpath'
|
||||
if loc.startswith('@'): # 根据属性查找
|
||||
@ -165,13 +167,14 @@ def translate_loc_to_xpath(loc) -> tuple:
|
||||
return loc_by, loc_str
|
||||
|
||||
|
||||
def avoid_duplicate_name(folder_path: str, file_name: str) -> str:
|
||||
"""检查文件是否重名,并返回可以使用的文件名
|
||||
def get_available_file_name(folder_path: str, file_name: str) -> str:
|
||||
"""检查文件是否重名,并返回可以使用的文件名 \n
|
||||
:param folder_path: 文件夹路径
|
||||
:param file_name: 要检查的文件名
|
||||
:return: 可用的文件名
|
||||
"""
|
||||
file_Path = Path(folder_path).absolute().joinpath(file_name)
|
||||
folder_path = Path(folder_path).absolute()
|
||||
file_Path = folder_path.joinpath(file_name)
|
||||
while file_Path.exists():
|
||||
ext_name = file_Path.suffix
|
||||
base_name = file_Path.stem
|
||||
@ -181,12 +184,12 @@ def avoid_duplicate_name(folder_path: str, file_name: str) -> str:
|
||||
file_name = f'{base_name.replace(f"({num})", "", -1)}({num + 1}){ext_name}'
|
||||
else:
|
||||
file_name = f'{base_name} (1){ext_name}'
|
||||
file_Path = Path(folder_path).joinpath(file_name)
|
||||
file_Path = folder_path.joinpath(file_name)
|
||||
return file_name
|
||||
|
||||
|
||||
def clean_folder(folder_path: str, ignore: list = None) -> None:
|
||||
"""清空一个文件夹,除了ignore里的文件和文件夹
|
||||
"""清空一个文件夹,除了ignore里的文件和文件夹 \n
|
||||
:param folder_path: 要清空的文件夹路径
|
||||
:param ignore: 忽略列表
|
||||
:return: None
|
||||
|
@ -14,7 +14,7 @@ from selenium.webdriver.remote.webelement import WebElement
|
||||
from selenium.webdriver.support import expected_conditions as ec
|
||||
from selenium.webdriver.support.wait import WebDriverWait
|
||||
|
||||
from .common import DrissionElement, get_loc_from_str, translate_loc_to_xpath, avoid_duplicate_name
|
||||
from .common import DrissionElement, get_loc_from_str, translate_loc_to_xpath, get_available_file_name
|
||||
|
||||
|
||||
class DriverElement(DrissionElement):
|
||||
@ -290,7 +290,7 @@ class DriverElement(DrissionElement):
|
||||
name = filename or self.tag
|
||||
path = Path(path).absolute()
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
name = avoid_duplicate_name(str(path), f'{name}.png')
|
||||
name = get_available_file_name(str(path), f'{name}.png')
|
||||
# 等待元素加载完成
|
||||
if self.tag == 'img':
|
||||
js = 'return arguments[0].complete && typeof arguments[0].naturalWidth != "undefined" ' \
|
||||
|
@ -14,7 +14,7 @@ from selenium.common.exceptions import NoAlertPresentException
|
||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
||||
from selenium.webdriver.remote.webelement import WebElement
|
||||
|
||||
from .common import get_loc_from_str, avoid_duplicate_name, translate_loc_to_xpath
|
||||
from .common import get_loc_from_str, get_available_file_name, translate_loc_to_xpath
|
||||
from .driver_element import DriverElement, execute_driver_find
|
||||
|
||||
|
||||
@ -296,7 +296,7 @@ class DriverPage(object):
|
||||
name = filename or self.title
|
||||
path = Path(path).absolute()
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
name = avoid_duplicate_name(str(path), f'{name}.png')
|
||||
name = get_available_file_name(str(path), f'{name}.png')
|
||||
img_path = f'{path}\\{name}'
|
||||
self.driver.save_screenshot(img_path)
|
||||
return img_path
|
||||
|
@ -14,7 +14,7 @@ from urllib.parse import urlparse, quote
|
||||
|
||||
from requests_html import HTMLSession, HTMLResponse, Element
|
||||
|
||||
from .common import get_loc_from_str, translate_loc_to_xpath, avoid_duplicate_name
|
||||
from .common import get_loc_from_str, translate_loc_to_xpath, get_available_file_name
|
||||
from .config import OptionsManager
|
||||
from .session_element import SessionElement, execute_session_find
|
||||
|
||||
@ -242,7 +242,7 @@ class SessionPage(object):
|
||||
elif file_exists == 'overwrite':
|
||||
pass
|
||||
elif file_exists == 'rename':
|
||||
full_name = avoid_duplicate_name(goal_path, full_name)
|
||||
full_name = get_available_file_name(goal_path, full_name)
|
||||
full_path = Path(f'{goal_path}\\{full_name}')
|
||||
else:
|
||||
raise ValueError("Argument file_exists can only be 'skip', 'overwrite', 'rename'.")
|
||||
|
Loading…
x
Reference in New Issue
Block a user