Identifying the element by using the XML path of the HTML structure to locate the element is known as XPath.
Identifying the MobileElement by “XPath”. 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.XPATH,"Locator_Value")
Second , We can call the direct id method of find_element.
Syntax :
driver.find_element_by_xpath("Locator_Value")
We have two types of XPath
Absolute XPath :
It uses a complete XML root path to the required MobileElement.
This Absolute XPath is not recommended because whenever the XML structure changes then we need to update the XPath as well every time.
Syntax :
/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.EditText
'/' - finding the element inside the parent element '//' - finding the child or nested-child element inside the parent element
Relative Xpath :
It uses the direct path of a MobileElement using (id,className,attribute values , sub-string,etc) to perform action on it.
Syntax :
//android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]
//tag[@attribute='value'] //android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]