0

I have a table with header Task details and TaskTime. Task details column contain some text (Perform Operation roll no. 150) and onclick there is java script operation is performed and new page is displayed in same window.

I want to click on particular table cell say 2nd or 3rd or any cell using partial text roll no. by using findelementBy.cssselector.

Below is my code snippet for table which gets frequently updates and different roll no. gets added to table

<tr class="Pointer" onmouseover="this.style.cursor='hand'" title="Perform" style="">
<td onclick="javascript:__doPostBack">Perform Operation roll no. 150</td>
<td onclick="javascript:__doPostBack">07 Jul 2015 05:26 PM</td>


<tr class="Pointer" onmouseover="this.style.cursor='hand'" title="Perform" style="">
<td onclick="javascript:__doPostBack">Perform Operation roll no. 161</td>
<td onclick="javascript:__doPostBack">07 Jul 2015 05:18 PM</td>


<tr class="Pointer" onmouseover="this.style.cursor='hand'" title="Perform" style="">
<td onclick="javascript:__doPostBack">Perform Operation roll no. 155</td>
<td onclick="javascript:__doPostBack">07 Jul 2015 05:13 PM</td>

Note my xpath is not working properly and below table will frequently changes.

zishan paya
  • 503
  • 1
  • 6
  • 29
  • If you ask me, xpath will easily solve your problem. As you said, you want to click on 2nd or 3rd of any cell using partial text roll number, so do you want locate that element with partial text containing the concerned roll number or any element with partial text containing any 'roll no.' will do ? – Subh Jul 08 '15 at 06:12
  • Your question is not clear . What is your xpath which is not working? – Bhupesh Jul 08 '15 at 06:13
  • @subh: Yes partial text using roll number will also do. As xpath is slow I would prefer to go with CSS with partial text. Bhupesh: My xpath is working for 1st row but if I want to click on 2nd or 3rd depends on roll no I am not able to make – zishan paya Jul 08 '15 at 06:24

2 Answers2

1

Please try the below xpath:

//td[contains(text(),'roll no. 161')]

This will locate the td element that has innerHTML/text as roll no. 161. You can always replace 161 with any other roll number you want to locate.

OR

In case you want to click on 2nd, 3rd or any other td element with respect to the text roll no. on the list, you can try these:

  • (//td[contains(text(),'roll no.')])[2]

This will locate the 2nd td element in the chronological order of DOM representation that contains innerHTML/text as roll no..

  • (//td[contains(text(),'roll no.')])[3]

This will locate the 3rd td element in the chronological order of DOM representation that contains innerHTML/text as roll no..

Similarly, you can replace the last [2] with any other number to locate the concerned element in the page.

Subh
  • 4,354
  • 1
  • 13
  • 32
  • Locator not found [error] locator not found: xpath=(//td[contains(text(),'roll no.')])[2] – zishan paya Jul 08 '15 at 08:23
  • Not sure why it is not working. I tried with the same HTML snippet so added in question, at my end; it's working fine here. – Subh Jul 08 '15 at 09:51
0

After going deep into css I am able to locate element inside selenium ide

css=tr.Pointer > td:contains(roll nos)

Above roll nos will change frequently so is the element locator.

zishan paya
  • 503
  • 1
  • 6
  • 29
  • 1
    Is it working fine with this css ? I don't think **[CSS3 supports locating elements using innerHTML/text](http://stackoverflow.com/a/29539901/4193730)** – Subh Jul 08 '15 at 09:58
  • Exactly same issue I found when trying in Java webdriver code. Its working fine in Selenium IDE but `driver.findElement(By.cssSelector("tr.Pointer > td:contains(roll nos)")` gave me InvalidSelectorException. In short contains is not supported in CSS3 when used in java code. Is there other workaround for contains? – zishan paya Jul 08 '15 at 10:19
  • Only way out. Try using the xpaths instead, that I had provided earlier, and let me know what issues are you facing please. – Subh Jul 08 '15 at 11:44