0

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

Joolz
  • 39
  • 5
  • 1
    Can you share the HTML area screenshot in question, and if you can provide the URL so that we can help in better way. – Gaurav Lad Mar 02 '22 at 14:45
  • @GauravLad what I'm looking for is a function that will output the parent element given a known child element for any given url, is that possible? – Joolz Mar 02 '22 at 15:28
  • Can you help me understand why you don't what it with xpath? – Gaurav Lad Mar 03 '22 at 04:34
  • @GauravLad because I'd like to use the same function in multiple screens of the app (which will get refactored in the future), if possible. Added the xpath of the relevant items in an edit, thanks! – Joolz Mar 03 '22 at 07:47

1 Answers1

0

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('./..')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352