'Getting parent item from child without Xpath
I have a list of devices on my app screen. Each webelement that represents a unique device has 2 children:
- (unique) device name
- On/Off status
Parent devices have the same id, so the way to select the correct device is to go through the children and check that device_name.text is the name I want.
I'm trying to write a functions that evaluates on off status for a specific device name without using xpath
def check_on_off_status(device_name:WebElement):
device = device_name.find_element_by_xpath('..') # we find the parent from device_name, and navigate from there
# power_status_element = get_child(device,power_status_id)
# return power_status_element.enabled
But I'm getting an exception when evaluating the xpath expression. How can I get the parent node if I only have the element?
Edit:
Parent (Device) Xpath
/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout /android.widget.FrameLayout/android.widget.LinearLayout /android.widget.FrameLayout/android.view.ViewGroup /androidx.viewpager.widget.ViewPager /androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout /android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView /android.view.ViewGroup[1]/android.widget.FrameLayout /android.view.ViewGroup/android.view.ViewGroup
Device Name Xpath: /hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.widget.TextView[1]
On/Off Icon (device name sibling) Xpath:
/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.widget.ImageView
Solution 1:[1]
You were close enough. You just need to append the ./
character within the xpath so the ..
can be referenced with respect to the WebElement device_name
as follows:
device = device_name.find_element_by_xpath('./..')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | undetected Selenium |