I am trying to send a text into a search box which will auto list the items matching the text . I want to select the records which is exactly matching the search text..
Example:
<!DOCTYPE html>
<html lang="en">
<head><title>Test Selection</title></head>
<body>
<ul id='navlist'>
<li>
<table style='border:1px solid black'><tr><td>1234</td><td>Johnas</td><td>Active</td></tr></table>
</li>
<li>
<table style='border:1px solid blue'><tr><td>0234</td><td>Philips</td><td>Active</td></tr></table>
</li>
<li>
<table style='border:1px solid green'><tr><td>2235</td><td>Joseph</td><td>InActive</td></tr></table>
</li>
</ul>
</body>
</html>
When I send the text 0234, it lists two records 1234 and 0234 . But I want select the second i.e 0234 alone.
I am able to select the first records using the xpath = “//ul/li/table”, but finding it difficult to locate the records exactly matching the sent text.
tried, contains(.,’0234′), contains(text()=’0234′), contains(Text()=’0234′) . But not working.
Does contains text() also checks for numeric.
2
I would use something like //ul/li[table//tr/td[1] = '0234']
to select the li
element that contains a table
with the first td
of a row having the value 0234
.
Example fiddle.
I don’t know why you had two results for your query, but perhaps you made a mistake and queried for the string 234
rather than 0234
.
To search for table
elements which contain a particular piece of text which might be spread throughout the entire table, including text that begins in one table cell and continues in another, you would use this:
/html/body/ul/li/table[contains(., '0234')]
In that expression, the expression contains(., '0234')
passes the table
element as the first parameter of the contains
function. Because the contains
function expects a string value, this has the effect of converting the entire table
element to a string value, by concatenating all the element’s descendant text nodes, e.g. for the tables in your examples the tests would effectively be:
contains('1234JohnasActive', '0234'),
contains('0234PhilipsActive', '0234'),
contains('2235JosephInActive', '0234')
Another approach would be to check if any one of the cells in the table
match the search string individually:
/html/body/ul/li/table[.//td[contains(., '0234')]]
This expression would check each table by finding all the descendant td
elements, and seeing if any of them contained the string 0234
. e.g. the tests would be:
contains('1234', '0234'),
contains('Johnas', '0234'),
contains('Active', '0234'),
contains('0234', '0234'),
contains('Philips', '0234'),
contains('Active', '0234'),
contains('2235', '0234'),
contains('Joseph', '0234'),
contains('InActive', '0234')
If you actually want to check if there’s an exact match between your search string and any of the cells in a table, then you should use the =
operator rather than the contains()
function, e.g.
/html/body/ul/li/table[.//td='0234']
The effective tests would be, for table 1:
'1234'='0234',
'Johnas'='0234',
'Active'='0234'
… for table 2 only one test would be performed, because the first td
in the table would return true()
and the other cells would not need to be checked.
'0234'='0234'
… for table 3:
'2235'='0234',
'Joseph'='0234',
'InActive'='0234'
Note that when using the contains()
function, the values are treated as strings, not as numeric values, because the contains()
function expects string-valued parameters.
However, it’s different when you compare an element to another value using the =
operator. The =
operator will have the effect of converting the element into a data type which matches the data type of the other operand. If you compare the element to a string, the test will be string equality, but if you compare it to a number, the test will be numeric equality. e.g. This expression will return the second table
(because its descendant <td>0234</td>
will be converted to the number 234
by the =
operator).
/html/body/ul/li/table[.//td=234]
… but the following expression will not return anything, because the =
operator will convert the <td>0234</td>
into the string 0234
, which is not equal to the string 234
:
/html/body/ul/li/table[.//td='234']