[英]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 的元素的x
和y
坐标?
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.