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):
|
class DrissionElement(object):
|
||||||
|
"""SessionElement和DriverElement的基类"""
|
||||||
|
|
||||||
def __init__(self, ele: Union[Element, WebElement]):
|
def __init__(self, ele: Union[Element, WebElement]):
|
||||||
self._inner_ele = ele
|
self._inner_ele = ele
|
||||||
|
|
||||||
@ -64,22 +66,22 @@ class DrissionElement(object):
|
|||||||
|
|
||||||
|
|
||||||
def get_loc_from_str(loc: str) -> tuple:
|
def get_loc_from_str(loc: str) -> tuple:
|
||||||
"""处理元素查找语句
|
"""处理元素查找语句 \n
|
||||||
查找方式:属性、tag name及属性、文本、xpath、css selector
|
查找方式:属性、tag name及属性、文本、xpath、css selector \n
|
||||||
=表示精确匹配,:表示模糊匹配,无控制字符串时默认搜索该字符串
|
=表示精确匹配,:表示模糊匹配,无控制字符串时默认搜索该字符串 \n
|
||||||
示例:
|
示例: \n
|
||||||
@class:ele_class - class含有ele_class的元素
|
@class:ele_class - class含有ele_class的元素 \n
|
||||||
@class=ele_class - class等于ele_class的元素
|
@class=ele_class - class等于ele_class的元素 \n
|
||||||
@class - 带class属性的元素
|
@class - 带class属性的元素 \n
|
||||||
tag:div - div元素
|
tag:div - div元素 \n
|
||||||
tag:div@class:ele_class - class含有ele_class的div元素
|
tag:div@class:ele_class - class含有ele_class的div元素 \n
|
||||||
tag:div@class=ele_class - class等于ele_class的div元素
|
tag:div@class=ele_class - class等于ele_class的div元素 \n
|
||||||
tag:div@text():search_text - 文本含有search_text的div元素
|
tag:div@text():search_text - 文本含有search_text的div元素 \n
|
||||||
tag:div@text()=search_text - 文本等于search_text的div元素
|
tag:div@text()=search_text - 文本等于search_text的div元素 \n
|
||||||
text:search_text - 文本含有search_text的元素
|
text:search_text - 文本含有search_text的元素 \n
|
||||||
text=search_text - 文本等于search_text的元素
|
text=search_text - 文本等于search_text的元素 \n
|
||||||
xpath://div[@class="ele_class"]
|
xpath://div[@class="ele_class"] \n
|
||||||
css:div.ele_class
|
css:div.ele_class \n
|
||||||
"""
|
"""
|
||||||
loc_by = 'xpath'
|
loc_by = 'xpath'
|
||||||
if loc.startswith('@'): # 根据属性查找
|
if loc.startswith('@'): # 根据属性查找
|
||||||
@ -165,13 +167,14 @@ def translate_loc_to_xpath(loc) -> tuple:
|
|||||||
return loc_by, loc_str
|
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 folder_path: 文件夹路径
|
||||||
:param file_name: 要检查的文件名
|
:param file_name: 要检查的文件名
|
||||||
:return: 可用的文件名
|
: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():
|
while file_Path.exists():
|
||||||
ext_name = file_Path.suffix
|
ext_name = file_Path.suffix
|
||||||
base_name = file_Path.stem
|
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}'
|
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 = Path(folder_path).joinpath(file_name)
|
file_Path = folder_path.joinpath(file_name)
|
||||||
return file_name
|
return file_name
|
||||||
|
|
||||||
|
|
||||||
def clean_folder(folder_path: str, ignore: list = None) -> None:
|
def clean_folder(folder_path: str, ignore: list = None) -> None:
|
||||||
"""清空一个文件夹,除了ignore里的文件和文件夹
|
"""清空一个文件夹,除了ignore里的文件和文件夹 \n
|
||||||
:param folder_path: 要清空的文件夹路径
|
:param folder_path: 要清空的文件夹路径
|
||||||
:param ignore: 忽略列表
|
:param ignore: 忽略列表
|
||||||
:return: None
|
: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 import expected_conditions as ec
|
||||||
from selenium.webdriver.support.wait import WebDriverWait
|
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):
|
class DriverElement(DrissionElement):
|
||||||
@ -290,7 +290,7 @@ class DriverElement(DrissionElement):
|
|||||||
name = filename or self.tag
|
name = filename or self.tag
|
||||||
path = Path(path).absolute()
|
path = Path(path).absolute()
|
||||||
path.mkdir(parents=True, exist_ok=True)
|
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':
|
if self.tag == 'img':
|
||||||
js = 'return arguments[0].complete && typeof arguments[0].naturalWidth != "undefined" ' \
|
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.chrome.webdriver import WebDriver
|
||||||
from selenium.webdriver.remote.webelement import WebElement
|
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
|
from .driver_element import DriverElement, execute_driver_find
|
||||||
|
|
||||||
|
|
||||||
@ -296,7 +296,7 @@ class DriverPage(object):
|
|||||||
name = filename or self.title
|
name = filename or self.title
|
||||||
path = Path(path).absolute()
|
path = Path(path).absolute()
|
||||||
path.mkdir(parents=True, exist_ok=True)
|
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}'
|
img_path = f'{path}\\{name}'
|
||||||
self.driver.save_screenshot(img_path)
|
self.driver.save_screenshot(img_path)
|
||||||
return img_path
|
return img_path
|
||||||
|
@ -14,7 +14,7 @@ from urllib.parse import urlparse, quote
|
|||||||
|
|
||||||
from requests_html import HTMLSession, HTMLResponse, Element
|
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 .config import OptionsManager
|
||||||
from .session_element import SessionElement, execute_session_find
|
from .session_element import SessionElement, execute_session_find
|
||||||
|
|
||||||
@ -242,7 +242,7 @@ class SessionPage(object):
|
|||||||
elif file_exists == 'overwrite':
|
elif file_exists == 'overwrite':
|
||||||
pass
|
pass
|
||||||
elif file_exists == 'rename':
|
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}')
|
full_path = Path(f'{goal_path}\\{full_name}')
|
||||||
else:
|
else:
|
||||||
raise ValueError("Argument file_exists can only be 'skip', 'overwrite', 'rename'.")
|
raise ValueError("Argument file_exists can only be 'skip', 'overwrite', 'rename'.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user