How to inspect element if there are multiple elements with same class name and doesn't have resource-id

↘锁芯ラ 提交于 2019-12-24 20:02:16

问题


<?xml version="1.0" encoding="UTF-8" standalone="true"?>

-<hierarchy rotation="0">


-<node bounds="[0,0][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.FrameLayout" resource-id="" text="" index="0">


-<node bounds="[0,0][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.LinearLayout" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.FrameLayout" resource-id="android:id/content" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.webkit.WebView" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="true" focused="true" focusable="true" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.webkit.WebView" resource-id="" text="Ionic App" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">

<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0"/>


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="1">


-<node bounds="[0,42][720,1280]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[0,42][720,80]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">

<node bounds="[631,42][720,80]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="true" enabled="true" clickable="true" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.Button" resource-id="" text="V 3.5" index="0"/>

</node>

<node bounds="[190,105][530,229]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.Image" resource-id="" text="Himalaya Image" index="1"/>

<node bounds="[288,262][432,369]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.Image" resource-id="" text="Leap Image" index="2"/>


-<node bounds="[35,483][687,628]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="3">


-<node bounds="[131,483][589,565]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[131,483][589,565]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[131,483][589,563]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">


-<node bounds="[131,483][589,563]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="0">

<node bounds="[145,504][575,539]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="true" enabled="true" clickable="true" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.EditText" resource-id="" text="" index="0" NAF="true"/>

<node bounds="[131,483][589,565]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="true" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="1" NAF="true"/>

</node>

</node>

</node>

</node>


-<node bounds="[35,596][687,628]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="1">

<node bounds="[224,602][390,628]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="true" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="Register" index="0"/>

<node bounds="[388,596][397,628]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text=" " index="1"/>

<node bounds="[393,602][497,628]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="true" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="Forgot PIN" index="2"/>

</node>

</node>

<node bounds="[101,745][619,1106]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.widget.Image" resource-id="" text="Lead 2020" index="4"/>

</node>

</node>

</node>

</node>

</node>

</node>

<node bounds="[0,42][720,45]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="" text="" index="1"/>

</node>

</node>

</node>

</node>

</node>

</node>

<node bounds="[0,0][720,42]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.talentpace.substk" class="android.view.View" resource-id="android:id/statusBarBackground" text="" index="1"/>

</node>

</hierarchy>

Inspected Element. Tried finding element in code by various ways. But nothing is working. Please suggest what should be the xpath for editing the PIN number in app

  1. android.view.view class has multiple elements, resource id and text is null.
  2. Only 1 element with class android.widget.editText. But that also doesnt work.

Attached the tree which i am trying to write the code for

UIAutomatorTree

Below is the xpath which i tried

driver.findElementByXPath("//android.widget.FrameLayout//android.view.View[@index='3']//android.widget.EditText")


回答1:


That's not answering the question but may help if you have access to the source code. setContentDescription method helps to deal with such problems, it has to be defined in XML which helps not only with accessibility but also during any UI testing. Even when you have an ID/text it may be a situation when multiple views have similar text and/or ids

In your case - dividing elements in the XPath should be done with one slash "/" Here is an example

driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.view.View[3]/android.widget.EditText[0]"));



回答2:


In your XPath expression, you are trying to get all <android.widget.FrameLayout[1]> elements. Looking to your xml, the only elements your document has is one <hierarchy> and multiple <node> elements. That's why you don't get any results.

Assuming you are coding in C#, try something like this:

driver.findElementByXPath(@"//node[@class = ""android.widget.EditText""]));
  • With the @ you tell the compiler to use string literals, so you don't need to escape characters.
  • //node returns all the <node> elements in the current xml document.
  • [@class = ""android.widget.EditText""] tells your XPath expression you need elements with a class attribute with a value that equals "android.widget.EditText".
  • The double quotation is needed so Visual Studio recognises "android.widget.EditText" as a string value.

Note that using // isn't a very efficient way of querying the xml. Always try to avoid it and narrow you XPath expression. A nice XPath examples list \ cheat sheet from Microsoft here. It should help you accomplish this.



来源:https://stackoverflow.com/questions/55530101/how-to-inspect-element-if-there-are-multiple-elements-with-same-class-name-and-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!