Reputation: 1
I am testing a webpage with Selenium IDE that contains a table. I would like to verify text in a table cell (row, column) via CSS selector. The HTML structure of the table is very simple. Here is a sample of the table where every row represents a different attribute of a person:
<html...>
<head>
</head>
<body>
<div class="pageBody">
<table>
<tbody>
<tr>
<td></td>
<td>AGE</td>
<td>49</td>
</tr>
<tr>
<td></td>
<td>WEIGHT</td>
<td>165</td>
</tr>
... and so on
In Selenium IDE, I am able to find "49" in the table row containing AGE using the following CSS selector:
(APPROACH 1) css=#pageBody table tbody tr:nth-child(1) td:nth-child(3)
However, if I am not certain every table will have the same number of rows (in the event of dynamically created tables based on data available for each person), I could change the pseudo selector from nth-child to contains:
(APPROACH 2) css=#pageBody table tbody tr:contains('AGE') td:nth-child(3)
The problem comes when I export these approaches as JUnit code. The following block of JUnit code works:
(using APPROACH 1)
WebDriver.findElement (By.cssSelector ("#pageBody table tbody tr:nth-child(1) td:nth-child(3)"))
where the following block of JUnit code fails:
(using APPROACH 2)
WebDriver.findElement (By.cssSelector ("#pageBody table tbody tr:contains('AGE') td:nth-child(3)"))
Does anyone know why the JUnit APPROACH 2 is failing to find the CSS selector where it is perfectly valid and works very well in Selenium IDE? Please ask questions for clarification. Thank you kindly for any light you can shed on this.
Upvotes: 0
Views: 1558
Reputation: 12806
As far as I know the selector :contains
is not supported in CSS. To select elements based on textual content you should instead use XPath.
Upvotes: 1