How to get the xpath to elements in the Table which is inside tag?

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']

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật