From ce78dade351f388dbb8d9a2452bf5c71b849b7fc Mon Sep 17 00:00:00 2001 From: Chris8711 Date: Fri, 1 Apr 2022 19:25:56 +0800 Subject: [PATCH 1/3] update: add property "active_element" to driver --- appium/webdriver/extensions/active_element.py | 42 +++++++++++++++++++ appium/webdriver/webdriver.py | 2 + 2 files changed, 44 insertions(+) create mode 100644 appium/webdriver/extensions/active_element.py diff --git a/appium/webdriver/extensions/active_element.py b/appium/webdriver/extensions/active_element.py new file mode 100644 index 00000000..f8df957e --- /dev/null +++ b/appium/webdriver/extensions/active_element.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from selenium.common.exceptions import NoSuchElementException +from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands +from selenium.webdriver.remote.command import Command as W3C_Command +from ..webelement import WebElement + + +class ActiveElement(CanExecuteCommands): + + @property + def active_element(self) -> WebElement: + """Retrieves session information from the current session + + Usage: + session = driver.session + + Returns: + `dict`: containing information from the current session + """ + try: + return self.execute(W3C_Command.W3C_GET_ACTIVE_ELEMENT)['value'] + except NoSuchElementException: + return None + + def send_keys_to_active_element(self, value: str): + return self.active_element.send_keys(value) + + def click_active_element(self): + return self.active_element.click() diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 7694365e..98059c4f 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -58,6 +58,7 @@ from .mobilecommand import MobileCommand as Command from .switch_to import MobileSwitchTo from .webelement import WebElement as MobileWebElement +from .extensions.active_element import ActiveElement # From remote/webdriver.py _W3C_CAPABILITY_NAMES = frozenset( @@ -249,6 +250,7 @@ class WebDriver( Settings, Sms, SystemBars, + ActiveElement ): def __init__( self, From b2bfe479a0574cab7e4d694a0c8be0f62a994a99 Mon Sep 17 00:00:00 2001 From: Chris8711 Date: Fri, 1 Apr 2022 20:12:15 +0800 Subject: [PATCH 2/3] modify property active_element to function get_active_element --- appium/webdriver/extensions/active_element.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/appium/webdriver/extensions/active_element.py b/appium/webdriver/extensions/active_element.py index f8df957e..a15673c0 100644 --- a/appium/webdriver/extensions/active_element.py +++ b/appium/webdriver/extensions/active_element.py @@ -20,15 +20,17 @@ class ActiveElement(CanExecuteCommands): - @property - def active_element(self) -> WebElement: - """Retrieves session information from the current session + def get_active_element(self) -> 'WebElement': + """ + + get the active element. - Usage: - session = driver.session + It usually used after tapping a point and active the element. + And use the element. Returns: - `dict`: containing information from the current session + + `WebElement`: web element """ try: return self.execute(W3C_Command.W3C_GET_ACTIVE_ELEMENT)['value'] @@ -36,7 +38,6 @@ def active_element(self) -> WebElement: return None def send_keys_to_active_element(self, value: str): - return self.active_element.send_keys(value) - - def click_active_element(self): - return self.active_element.click() + element = self.get_active_element() + if element: + return element.clear().send_keys(value) From 3a55f94b3ad8a75af1bc55a3304ffacf50ee5ce3 Mon Sep 17 00:00:00 2001 From: Chris8711 Date: Fri, 1 Apr 2022 20:35:42 +0800 Subject: [PATCH 3/3] update: when get active element error, throw the NoSuchElementException --- appium/webdriver/extensions/active_element.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/appium/webdriver/extensions/active_element.py b/appium/webdriver/extensions/active_element.py index a15673c0..a1b4ded9 100644 --- a/appium/webdriver/extensions/active_element.py +++ b/appium/webdriver/extensions/active_element.py @@ -32,12 +32,11 @@ def get_active_element(self) -> 'WebElement': `WebElement`: web element """ - try: - return self.execute(W3C_Command.W3C_GET_ACTIVE_ELEMENT)['value'] - except NoSuchElementException: - return None + return self.execute(W3C_Command.W3C_GET_ACTIVE_ELEMENT)['value'] def send_keys_to_active_element(self, value: str): - element = self.get_active_element() - if element: + try: + element = self.get_active_element() return element.clear().send_keys(value) + except NoSuchElementException: + return None