views:

111

answers:

1

click //div[15]/div/ul/li[5]

In the snippet above, how can I write xpath or css selector based on known li item. Yes, I know li item ahead of time(such as @, +, * etc ) Also there is no guarantee that div[15] would be always div[15] it could be div[14] or div[17] etc

A: 

Would need more information to give a good answer, but some suggestions are below:

If you want to select the 5th <li> in the entire HTML document:

xpath=/descendant::li[5]

I'm not sure what you mean by "I know li item ahead of time(such as @, +, * etc )" but if you know a unique attribute of the <li> you can use that to locate it:

If you know the @id of the <li>:

id=knownId

If you know a CSS class of the <li>:

css=li.knownClass
xpath=//li[contains(@class, 'knownClass']

If you know the text of the <li>:

css=li:contains(knownText)
xpath=li[contains(text(), 'knownText')]

Hope that helps. Some more information - ideally a snippet of you HTML - would help to give a more specific answer.

Dave Hunt
Hi Dave, thank you for your response with multiple answer options. I tried following click("css=li:contains("+variable +")"); //doesn't workclick(//div[15]/div/ul/li[contains(text(), '"+variable +"')]"); //workand second command works but I have to pass DIV[IndexNumber] which in this case is DIV[15] which I'm not happy about it
doneright
Can you provide a snippet of your HTML?
Dave Hunt
li dynamic menu is accessed by clicking on span[2] which is either one of item in list menu.In following scenario, I click on span[2], then dynamic menu opens where it allows me to choose one of value from it's menu. In following scenario, I repeated same procedure couple of time.
doneright
<tr> <td>click</td> <td>//div[@id='myid']/div/div/span[2]</td> <td></td></tr><tr> <td>click</td> <td>//div[21]/div/ul/li[5]</td> <td></td></tr><tr> <td>click</td> <td>//div[@id='myid']/div/div/span[2]</td> <td></td></tr><tr> <td>click</td> <td>//div[22]/div/ul/li[7]</td> <td></td></tr></tbody></table></body></html>
doneright
Could you provide the relevant HTML source from your application? I suggest adding it to your original question for readability.
Dave Hunt