From 23e86b17a3486088770673c337dd7ae500742368 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 22 Feb 2021 16:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dclick=5Fat()=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/driver_element.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index cae78e9..7344a98 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -352,14 +352,24 @@ class DriverElement(DrissionElement): :param by_js: 是否用js点击 :return: None """ - x = self.location['x'] + x if x is not None else self.location['x'] + self.size['width'] // 2 - y = self.location['y'] + y if y is not None else self.location['y'] + self.size['height'] // 2 - if by_js: - self.page.run_script(f'document.elementFromPoint({x}, {y}).click();') + x = self.location['x'] + x if x is not None else self.location['x'] + self.size['width'] // 2 + y = self.location['y'] + y if y is not None else self.location['y'] + self.size['height'] // 2 + js = f""" + var ev = document.createEvent('HTMLEvents'); + ev.clientX = {x}; + ev.clientY = {y}; + ev.initEvent('click', false, true); + arguments[0].dispatchEvent(ev); + """ + self.run_script(js) + else: + x = x if x is not None else self.size['width'] // 2 + y = y if y is not None else self.size['height'] // 2 + from selenium.webdriver import ActionChains - ActionChains(self.page.driver).move_by_offset(x, y).click().perform() + ActionChains(self.page.driver).move_to_element_with_offset(self.inner_ele, x, y).click().perform() def input(self, value: Union[str, tuple], clear: bool = True) -> bool: """输入文本或组合键 \n