ExplicitWait or WebdriverWait is defined as the maximum time to wait for a given condition before throwing an error.
It takes a total of four arguments.
After creating WebDriverWait, Now we need to call until() method which helps to wait until WebElement is present on the screen and perform the action on it.
Syntax :
wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,NoSuchElementException]) wait.until(expected_conditions.presence_of_element_located((AppiumBy.Locator_Type,"Locator_Value")))
If you declare ExplicitWait with a condition presence_of_element_located and the max wait time is 25 seconds , if MobileElement is visible less than 25 seconds then it will click or do the respective operation on that particular MobileElement. If an element is not found on the device it will wait for 25 seconds and throw an error.
Example: In the below example we are launching the app on an Android device and waiting for an element for max time of 25 seconds to perform action on it.
ExplicitwaitEx.py
from appium import webdriver from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException from selenium.webdriver.support.wait import WebDriverWait from appium.webdriver.common.appiumby import AppiumBy import time # Step 1 : Create "Desired Capabilities" desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['automationName'] = 'UiAutomator2' desired_caps['platformVersion'] = '10' desired_caps['deviceName'] = 'Pixel3XL' desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk') desired_caps['appPackage'] = 'com.skill2lead.appiumdemo' desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity' # Step 2 : Create "Driver object" driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # Step 3 : Create "WebDriver object" wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,ElementNotSelectableException,NoSuchElementException]) # Step 4 : "Click on the button using Webdriver and text locator value" ele = wait.until(lambda x: x.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'text("ENTER SOME VALUE")')) ele.click() # Step 5 : Wait for 2 seconds time.sleep(4) # Step 6 : Close the driver object driver.quit()
Example 2: In this example we are using explicit wait along with other locator types such as id , class name , content-description etc.
ExplicitwaitEx.py
from appium import webdriver from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException from selenium.webdriver.support.wait import WebDriverWait from appium.webdriver.common.appiumby import AppiumBy import time # Step 1 : Create "Desired Capabilities" desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['automationName'] = 'UiAutomator2' desired_caps['platformVersion'] = '10' desired_caps['deviceName'] = 'Pixel3XL' desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk') desired_caps['appPackage'] = 'com.skill2lead.appiumdemo' desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity' # Step 2 : Create "Driver object" driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # Step 3 : Create "WebDriver object" # list of Selenium Exceptions wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,ElementNotSelectableException,NoSuchElementException]) #ele = wait.until(lambda x: x.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")) #ele.click() #ele = wait.until(lambda x: x.find_element(AppiumBy.CLASS_NAME,"android.widget.EditText")).send_keys("Code2Lead") #ele = wait.until(lambda x: x.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'UiSelector().description("Btn3")')) ele = wait.until(lambda x: x.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'text("ENTER SOME VALUE")')) ele.click() time.sleep(4) driver.quit()