1

I created XPath for a dynamic web element and it found 2 very identical nodes.The XPath is://*[contains(@id, "gwt-uid-")]/span[2]. I have done everything possible to differentiate uniquely between the two nodes but no luck. The HTML is attached Can anyone help?

enter image description here

Testilla
  • 602
  • 8
  • 21
  • first, identify the nearest static element and then move to the element you required using XPath using XPATH Axes to traverse among HTML elements. – Naveen Kumar R B Oct 24 '16 at 13:59
  • Ideally, use other stable attributes or a combination of them to ensure that it works. You'd be able to get specific help with the element if you include a snippet of your HTML in the question. – Sai Oct 24 '16 at 14:20
  • Could you share relevant HTML with 2-3 level above HTML?? – Saurabh Gaur Oct 24 '16 at 15:50
  • @ Saurabh Gaur, @Sai please refer to the attached html above. Thanks – Testilla Oct 25 '16 at 10:05

1 Answers1

0
  1. To macth first value (Example -> text in span tag):

    //div[contains(@id, 'gwt-uid-') and @aria-level="1"]/div/div/span[2]

  2. To match second value (Example1 -> text in span tag)

    //div[contains(@id, 'gwt-uid-') and @aria-level="2"]/div/div/span[2]

try the following XPath to match partial id:

//input[contains(@id, 'gwt-uid-')]
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Naveen, well done! The the XPath you supplied worked. First please a give details of how the xpath was generated so I can handle similar scenarios in future. Second, the xpath did identify the web element I was looking for which is a dropdown element. When webdriver clicked on the dropdown the embedded items were displayed. Now, I do I choose one of the items. The all have html span tags like :Shoe and the xpath looks like this: .//*[@id='VAADIN_COMBOBOX_OPTIONLIST']/div/div[2]/table/tbody/tr[2]/td/span – Testilla Oct 24 '16 at 15:53
  • contains is a partial string matching method available in XPATH. I would suggest you go through the following link to fully understand what you can do with XPATH, http://www.w3schools.com/xml/xpath_intro.asp (all links under XPATH tutorial) and use Firebug and Firepath plugins (available to Firefox browser) to instantly check whether your XPath is working or not. – Naveen Kumar R B Oct 24 '16 at 15:58
  • Naveen,following the scenario above, how do I select a dropdown item? – Testilla Oct 24 '16 at 15:59
  • Always try to get the unique combination to match an element using XPATH using the element HTML code itself (using tag, attributes, text etc). For selecting one element one way is to click on one element from list of elements from the dropdown menu. that you can achieve by finding one of the values from the dropdown list as follows /use index to match one element. PLEASE SHARE HTML CODE containing list of elements & its parents (2-3 levels) – Naveen Kumar R B Oct 24 '16 at 16:07
  • see the answer of http://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium-webdriver-using-java – Naveen Kumar R B Oct 24 '16 at 16:11
  • you can try out the methods which suits your needs best: https://www.w3.org/TR/xpath/#corelib – Naveen Kumar R B Oct 24 '16 at 16:13
  • Look at this changing xpath whose ID similar to above is dynamic as the number keeps changing. Will "contains" work in this case? .//*[@id='gwt-uid-361']/span[2] – Testilla Oct 24 '16 at 16:41
  • Yes, Definitely! Use //*[contains(@id, "gwt-uid-")]/span[2] (Use tag instead of * to get more unique) – Naveen Kumar R B Oct 24 '16 at 16:51
  • you used an input tag instead of div. I tried the "input" and as expected, it did not work, so I changed it to "div" and banged, it worked like a charm. You did really well with this XPath. Thank you. – Testilla Oct 25 '16 at 11:11
  • yeah. I forgot to edit that ;-). edited now. thanks for the correction. – Naveen Kumar R B Oct 25 '16 at 11:15
  • please refer to http://stackoverflow.com/questions/40290308/xpath-matching-one-node-not-working-in-webdriver – Testilla Oct 27 '16 at 16:57