1

WebApplication has Calender Ajax Control twice on same page. I cannot use id's as ids get generated by Ajax call dynamically . So identifying it using xpaths and classnames. I can identify First calender controll uniquely but when I click on second , Ajax call generates similar div for second call , so those locators become invalid.

Can anybody suggest how to tracel this scenario.

Thanks a lot.

0

If the elements are completely equal (in terms of nodes and attributes), one way to tell them apart is with the Xpath predicate last() or simply call the n-th element with [n].

For example, if you know the second calendar is added below the first, you can use either of the above, depending on your test.

See also http://www.w3schools.com/xpath/xpath_syntax.asp

  • I used last() function and it worked like charm :) ..here is my sample xpath (.//*[@class='x-date-middle']//td[@class='x-btn-center']//button[@class='x-btn-text'])[last()] and as I supposed to iterate through Table ..i just used last function with table's xpath to identify last table and based on that iterated through rows and columns ... – amb Jun 4 '15 at 9:20
  • I have use css path with nth-child. But is is not working for same level element. You will found both date at nth:child(1). – Sagar007 Jun 4 '15 at 9:32
0

As I read your question I have found this scenario while I am practicing automation.

<div class = 'abc' id='*dynamic*'>
  <div class = 'pqr'>date1</div>
</div>
<div class = 'abc' id='*dynamic*'>
  <div class = 'pqr'>date2</div>
</div>

-Here you need to understand that you have all div as same structure.

-So you can use xpath only.

-Let`s try this this way.

-wrong Xpath : //div[@class='abc']/div[@class='pqr'][1]

-true Xpath : (//div[@class='abc']/div[@class='pqr'])[1]

-Here [1] is for first child with same place.

-This xpath is find first class=abc and then found class=pqr then it returns list of two element.

-This list can not be handled by wrong xpath.

-Wrong xpath gives two element.

-For take first date picker :

 (//div[@class='abc']/div[@class='pqr'])[1]

-For take second date picker :

  (//div[@class='abc']/div[@class='pqr'])[2]

-This will sure your problem for sure.

  • yes it was working ..but I wanted to make it dynamically without giving any hard coded value like (//div[@class='abc']/div[@class='pqr'])[1] and (//div[@class='abc']/div[@class='pqr'])[2] where [1] and [2] i mean hardcoded value – amb Jun 4 '15 at 9:23
  • Dynamic in what term ? It is working fine that is very good news for me and as well for you. Thank you for that. As I learn from DOM model. Some element are static. You have to assume. If they are not then you have to try in different way. You can use this xpath. Because ultimately it will find first and second element as you mention in question. – Sagar007 Jun 4 '15 at 9:31
  • what you mean is also right ..but what if the things keep on adding and total divs become more than 2 and ..and I need to access only updated(newly added/last ) div ..in this scnario one will prefer last() function . – amb Jun 4 '15 at 13:45
  • It is okay if last() function is works properly.Thank you for question. – Sagar007 Jun 5 '15 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.