From 727ab0e00678eaff445b644a6251386ac0ad0ca7 Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 10 Jun 2021 15:12:19 +0800 Subject: [PATCH] =?UTF-8?q?DriverElement=E5=A2=9E=E5=8A=A0set=5Fproperty()?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/driver_element.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index f8c6e1c..b07bf24 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -6,13 +6,14 @@ """ import re from pathlib import Path +from time import sleep +from typing import Union, List, Any, Tuple + from selenium.common.exceptions import TimeoutException, JavascriptException, InvalidElementStateException from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.wait import WebDriverWait -from time import sleep -from typing import Union, List, Any, Tuple from .common import DrissionElement, str_to_loc, get_available_file_name, translate_loc, format_html @@ -416,7 +417,7 @@ class DriverElement(DrissionElement): def clear(self) -> None: """清空元素文本""" self.run_script("arguments[0].value=''") - # self.ele.clear() + # self.inner_ele.clear() def is_selected(self) -> bool: """是否选中""" @@ -462,12 +463,25 @@ class DriverElement(DrissionElement): return img_path - def set_attr(self, attr: str, value: str) -> bool: - """设置元素属性 \n - :param attr: 属性名 + def set_property(self, prop: str, value: str) -> bool: + """设置元素property属性 \n + :param prop: 属性名 :param value: 属性值 :return: 是否设置成功 """ + try: + value = value.replace("'", "\\'") + self.run_script(f"arguments[0].{prop}='{value}';") + return True + except: + return False + + def set_attr(self, attr: str, value: str) -> bool: + """设置元素attribute参数 \n + :param attr: 参数名 + :param value: 参数值 + :return: 是否设置成功 + """ try: self.run_script(f"arguments[0].setAttribute(arguments[1], arguments[2]);", attr, value) return True