简体   繁体   English

如何使用 Python 中的 Appium 从“x”和“y”坐标中获取 XPATH?

[英]How can one get the XPATH from the 'x' and 'y' coordinates using Appium in Python?

Is there any possible way to get the xpath for a mobile app given just the x and y coordinates for the element using Appium and Python?是否有任何可能的方法来获取移动应用程序的xpath ,仅给出使用 Appium 和 Python 的元素的xy坐标?

Actually it cannot be done in Appium in general way.实际上它不能以一般方式在 Appium 中完成。 And there is no such keyword to do this.并且没有这样的关键字来做到这一点。

But you can do this yourself as the following.但是您可以按照以下方式自己执行此操作。

1- Get the page source 1-获取页面源

page_src = driver.page_source

2- Get the all elements coordinate attribute in a dictionary or list 2-获取字典或列表中的所有元素坐标属性

import xml.etree.ElementTree as el_tree

page_src = driver.page_source
element_coords={}
page_src_xml = el_tree.fromstring(page_src)
for element in page_src_xml.iter():
   element_bounds = element.attrib['bounds']
   coords = {}

# calculate center of elements (which is x,y of element)

   bounds = element_bounds.replace('[', '').replace(']', ',').split(',')
   coords.update({'x': (float(bounds[0]) + float(bounds[2])) / 2})
   coords.update({'y': (float(bounds[1]) + float(bounds[3])) / 2})

# coords is what you need for next steps

3- Try to compare the coords (from step 2) with your desired, in a for or while loop. 3-尝试在 for 或 while 循环中将坐标(来自步骤 2)与您想要的进行比较。

4 - when you find the element with the desired coords, you simply can return the element and act on it. 4 - 当您找到具有所需坐标的元素时,您只需返回该元素并对其进行操作。

5 - Here you don't need to get XPATH for doing something on element, because you already locate it. 5 - 在这里,您无需获取 XPATH 即可对元素进行操作,因为您已经找到了它。

But if you need the element xpath for further usage, I'm afraid you can get the XPATH from element, but the other option like "text" or "content-desc" maybe available.但是,如果您需要元素 xpath 以供进一步使用,恐怕您可以从元素中获取 XPATH,但其他选项如“文本”或“内容描述”可能可用。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python获取x,y坐标? - How to get x,y coordinates using python? 蟒蛇。 如何从ax,y列表和偏移距离获取偏移样条的x,y坐标 - Python. How to get the x,y coordinates of a offset spline from a x,y list of points and offset distance 如何从文件中按 x 排序 (x, y) 坐标 - How to get sorted (x, y) coordinates by x from a file 使用(x,y)坐标到达特定的“村庄”(Python 3) - Using (x,y) coordinates to get to a specific “village” (Python 3) 如何从python中的2D列表中获取最大x和最小y坐标? - How to get the max x and min y coordinates from a 2D list in python? 如何使用Python从地理数据中提取x,y和z坐标? - How can I extract x, y and z coordinates from geographical data by Python? 如何使用 python 在 opencv 中打印 x 和 y 坐标? - How to print x and y coordinates in opencv using python? 如何使用Python在网格中创建10个随机的x,y坐标 - How to create 10 random x, y coordinates in a grid using Python 如何从Python中的一系列图像生成一系列(x,y)坐标? - How to generate a series of (x,y) coordinates from a series of images in Python? pygame - 如何从get_pos中分割X和Y坐标 - pygame - How to split the X and Y coordinates from get_pos
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM