mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-05 05:28:43 +08:00
派生WebDriverWait的之类,用于抛出需要的异常
This commit is contained in:
parent
f3919263b6
commit
6a581c2a45
@ -4,6 +4,7 @@
|
||||
@Contact : g1879@qq.com
|
||||
@File : common.py
|
||||
"""
|
||||
import time
|
||||
from abc import abstractmethod
|
||||
from pathlib import Path
|
||||
from re import split as re_SPLIT
|
||||
@ -11,7 +12,9 @@ from shutil import rmtree
|
||||
from typing import Union
|
||||
|
||||
from requests_html import Element
|
||||
from selenium.common.exceptions import TimeoutException, InvalidSelectorException
|
||||
from selenium.webdriver.remote.webelement import WebElement
|
||||
from selenium.webdriver.support.wait import WebDriverWait
|
||||
|
||||
|
||||
class DrissionElement(object):
|
||||
@ -73,6 +76,30 @@ class DrissionElement(object):
|
||||
# pass
|
||||
|
||||
|
||||
class WebDriverWaitPlus(WebDriverWait):
|
||||
"""重写until方法,使其在接收到InvalidSelectorException异常时跳出"""
|
||||
|
||||
def until(self, method, message=''):
|
||||
screen = None
|
||||
stacktrace = None
|
||||
|
||||
end_time = time.time() + self._timeout
|
||||
while True:
|
||||
try:
|
||||
value = method(self._driver)
|
||||
if value:
|
||||
return value
|
||||
except self._ignored_exceptions as exc:
|
||||
if isinstance(exc, InvalidSelectorException):
|
||||
raise exc
|
||||
screen = getattr(exc, 'screen', None)
|
||||
stacktrace = getattr(exc, 'stacktrace', None)
|
||||
time.sleep(self._poll)
|
||||
if time.time() > end_time:
|
||||
break
|
||||
raise TimeoutException(message, screen, stacktrace)
|
||||
|
||||
|
||||
def get_loc_from_str(loc: str) -> tuple:
|
||||
"""处理元素查找语句 \n
|
||||
查找方式:属性、tag name及属性、文本、xpath、css selector \n
|
||||
|
Loading…
Reference in New Issue
Block a user