diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index 253bb92..b9a3f0e 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -69,7 +69,7 @@ class Drission(object): if isinstance(self._driver_options, dict): options = _dict_to_chrome_options(self._driver_options) else: - raise KeyError('Driver options invalid') + raise TypeError('Driver options invalid') if self._proxy: options.add_argument(f'--proxy-server={self._proxy["http"]}') diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index 9e9e333..0309082 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -122,7 +122,7 @@ class DriverElement(DrissionElement): elif isinstance(loc_or_str, tuple) and len(loc_or_str) == 2: loc_or_str = translate_loc_to_xpath(loc_or_str) else: - raise ValueError('loc_or_str must be tuple or str.') + raise ValueError('Argument loc_or_str can only be tuple or str.') if loc_or_str[0] == 'xpath': # 确保查询语句最前面是. @@ -162,8 +162,9 @@ class DriverElement(DrissionElement): self.clear() self.inner_ele.send_keys(value) return True - except: - raise + except Exception as e: + print(e) + return False def run_script(self, script: str) -> Any: """运行js""" @@ -237,8 +238,9 @@ class DriverElement(DrissionElement): try: self.run_script(f"arguments[0].{attr} = '{value}';") return True - except: - raise + except Exception as e: + print(e) + return False def drag(self, x: int, y: int, speed: int = 40, shake: bool = True) -> bool: """拖拽当前元素到相对位置 @@ -269,7 +271,7 @@ class DriverElement(DrissionElement): elif isinstance(ele_or_loc, tuple): target_x, target_y = ele_or_loc else: - raise KeyError('Need DriverElement, WebElement object or coordinate information.') + raise TypeError('Need DriverElement, WebElement object or coordinate information.') current_x = self.location['x'] + self.size['width'] // 2 current_y = self.location['y'] + self.size['height'] // 2 @@ -317,7 +319,7 @@ def execute_driver_find(page_or_ele: Union[WebElement, WebDriver], """ mode = mode or 'single' if mode not in ['single', 'all']: - raise ValueError("mode must be 'single' or 'all'.") + raise ValueError("Argument mode can only be 'single' or 'all'.") msg = result = None try: wait = WebDriverWait(page_or_ele, timeout=timeout) diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index 5be2a5f..7f5caa2 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -106,7 +106,7 @@ class DriverPage(object): :return: 等待是否成功 """ if mode.lower() not in ['del', 'display', 'hidden']: - raise ValueError('mode can only be "del", "display", "hidden"') + raise ValueError('Argument mode can only be "del", "display", "hidden"') from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as ec @@ -259,8 +259,8 @@ class DriverPage(object): elif mode == 'right': self.driver.execute_script(f"window.scrollBy({pixel},0);") else: - raise KeyError( - "mode must be selected among 'top','bottom','rightmost','leftmost','up','down','left','right'.") + raise ValueError( + "Argument mode can only be 'top', 'bottom', 'rightmost', 'leftmost', 'up', 'down', 'left', 'right'.") def refresh(self) -> None: """刷新页面""" @@ -276,7 +276,7 @@ class DriverPage(object): self.driver.maximize_window() else: if x <= 0 or y <= 0: - raise KeyError('x and y must greater than 0.') + raise ValueError('Arguments x and y must greater than 0.') new_x = x or self.driver.get_window_size()['width'] new_y = y or self.driver.get_window_size()['height'] self.driver.set_window_size(new_x, new_y) diff --git a/DrissionPage/mix_page.py b/DrissionPage/mix_page.py index 64d6d3e..a876d4a 100644 --- a/DrissionPage/mix_page.py +++ b/DrissionPage/mix_page.py @@ -53,7 +53,7 @@ class MixPage(Null, SessionPage, DriverPage): elif mode == 'd': self._driver = True else: - raise KeyError("mode must be 'd' or 's'.") + raise ValueError("Argument mode can only be 'd' or 's'.") @property def url(self) -> str: @@ -163,9 +163,9 @@ class MixPage(Null, SessionPage, DriverPage): path = download_path or self._drission.driver_options['experimental_options']['prefs'][ 'download.default_directory'] if not path: - raise KeyError - except KeyError: - raise KeyError('Download path not found.') + raise + except: + raise IOError('Download path not found.') return super().chrome_downloading(path) diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index 3feff94..b233ff8 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -99,7 +99,7 @@ class SessionElement(DrissionElement): elif isinstance(loc_or_str, tuple) and len(loc_or_str) == 2: loc_or_str = translate_loc_to_xpath(loc_or_str) else: - raise ValueError('loc_or_str must be tuple or str.') + raise TypeError('Type of loc_or_str can only be tuple or str.') loc_str = None if loc_or_str[0] == 'xpath': @@ -161,7 +161,7 @@ def execute_session_find(page_or_ele: BaseParser, """ mode = mode or 'single' if mode not in ['single', 'all']: - raise ValueError("mode must be 'single' or 'all'.") + raise ValueError("Argument mode can only be 'single' or 'all'.") loc_by, loc_str = loc msg = result = first = None try: