在没有 Xpath 的情况下从子项获取父项
我的应用程序屏幕上有一个设备列表。代表唯一设备的每个 WebElement 有 2 个子级:(
- 唯一)设备名称
- 开/关状态
父设备具有相同的 id,因此选择正确设备的方法是遍历子级并检查 device_name.text 是否为名称我想。
我正在尝试编写一个函数,在不使用 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
但在评估 xpath 表达式时出现异常。如果我只有元素,如何获取父节点?
编辑:
父(设备)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
设备名称 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 .widg et.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]
开/关图标(设备名称兄弟)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 .wid get.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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你已经足够接近了。您只需在 xpath 中附加
./
字符,这样..
可以参考 WebElementdevice_name
如下:You were close enough. You just need to append the
./
character within the xpath so the..
can be referenced with respect to the WebElementdevice_name
as follows: