Identifying the MobileElement by “id ” which is a unique parameter. It takes String as an argument. If an element isn't identified it throws an exception as No Such Element Exception.
We can write two ways to find element by using find_element() method. Let us discuss one by one.
First , We can identify mobile elements by using By class and locator type in find_element() method.
Syntax : driver.find_element(By.ID,"Locator_Value")
Second , We can call the direct id method of find_element.
Syntax : driver.find_element_by_id("Locator_Value")
Type-1 , Let us see type 1 how to click on the button using the ID locator value.
Example: In the below example we are launching the app on an Android device and clicking on a button using id locator.
ByID_Type1.py
from appium import webdriver import time from selenium.webdriver.common.by import By # 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 : "Click on the button using ID locator value" ele_id = driver.find_element(By.ID,"com.skill2lead.appiumdemo:id/EnterValue") ele_id.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()
Type-2 , Let us see type 2 how to click on the button using the ID locator value.
Example: In the below example we are launching the app on an Android device and clicking on a button using id locator.
ByID_Type2.py
from appium import webdriver 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 : "Click on the button using ID locator value" ele_id = driver.find_element_by_id("com.skill2lead.appiumdemo:id/EnterValue") ele_id.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()