This question might be a little specific but the test program im writing uses XPath to find the data i need in HTML. This piece of HTML(found here) is what im trying to parse.
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="textSm" align="right">1. </td> <!-- Location of the number is here -->
<td align="left" nowrap>
P Sandoval <!-- Player location is here of the number is here -->
</td>
</tr>
</table>
My goal is to find the name of the person by using the number that corresponds to him to find him. This requires me to find a node by the specific text contained in "td class="textSm" align="right">1. </td>" and then find the sibling of that node "<td align="left" nowrap>" then find the child of that sibling "P Sandoval" to get the desired result. I was wondering what kind of query I could use to find this. Any help is very much appreciated.
Use:
table/tr/td[starts-with(., '1.')]/following-sibling::td/a
This assumes that the context (current node) against which the XPath expression above is evaluated, is the parent of table.
//tr[td = "1"]/td[2]/a
For all TRs which have a TD equal to '1', give from the second child TD the A element.
Related
My HTML code looks like:
Table 1: And Table 2
<table id="ContentPlaceHolder_Body0">
<tbody>
<tr align="Center">
<td style="width:250px;">Some Name</td>
</tr>
</tbody>
</table>
<table id="ContentPlaceHolder_Body1">
<tbody>
<tr align="Center">
<td style="width:250px;">Some Name</td>
</tr>
</tbody>
</table>
When I use //*[contains(text(),'Some Name')] this xpath selects 2 values while I need to select only one value by Text.
I need to use that xpath with Text.
I'm trying to create xpath to get the "Some Name" from Table 2, element locator by using Text.
So I created this Xpath:
//table[#id='ContentPlaceHolder_Body1']//tr//td[#text()='Some Name']
This xpath is not working, need help to create correct xpath.
Given your HTML, this XPath works for me
//table[#id='ContentPlaceHolder_Body1']//td[text()='Some Name']
I think the issue with your XPath is that you were using #text() when it should be text().
//table[#id='ContentPlaceHolder_Body1']//tr//td[#text()='Some Name']
^ remove the #
You need to just remove the #
//table[#id='ContentPlaceHolder_Body1']//tr//td[text()='Some Name']
I have an issue with selecting a table to be read in Selenium.
I have a table in which there is two 'tr' elements inside the 'thead', and I need to find a way to ignore the first of these.
Here is the code:
<table class="noselect">
<thead>
<tr>
<th> </th>
<th class="number IOL">Interest Rates</th>
<th class="number IO">
</tr>
<tr>
<th>Description</th>
<th class="number">Value</th>
<th class="number">Percentage</th>
</tr>
</thead>
<tbody>
<tr>
<tr class="">
<tr class="">
<tr class="">
<tr>
<tr>
</tbody>
</table>
Using Selenium I will ask it to record the value of a certain row and column . This will then look at the Table element I will give it (hopefully using an XPath I can get working in this case), look at the thead and record the headers of each column. In this case that I am struggling with, the fact there is an extra 'tr' at the top of this table gets in the way of this process.
This is how the element is currently used:
[TableAlias("Detailed table")]
protected virtual IWebElement DetailedTable()
{
return Driver.FindElement(By.XPath("//table[#class='noselect']"));
}
I have tried many different ways which I can't get to work, but the gist of what I've been going for is:
//table[#class='noselect movements']/thead/tr/th[not(text()='Interest Rates')]/../../..
Here I'm stuck on going to the 'tr' element, telling it not to use it then backing out, but that selects it back again - and even that doesn't unselect the whole 'tr' element. It doesn't seem to help (to me) that the 'tr' element I'm trying to remove is blank with no class or defining features.
Is there a way of selecting the entire table except for the first 'tr' element in 'thead' as one element?
combine two xpathes. The 1st xpath take thead without the 1st tr and the 2nd tbody
//table/thead/tr[not(position()=1)] | //table/tbody
In your function that processes the THEAD tag, get the collection of TRs and start with [1] (skipping [0]) and process the rest.
I am working on automating a webpage editing task. The webpage has a dynamic table inside a frame. Each row has five columns. By dynamic table I mean that the rows are loaded dynamically on the web page. At any given point, the number of rows loaded are not more than 22. I can access more rows by moving the scroll bar. The total number of rows in the table is ~450, and I need to read all of them to modify some values in Column 2.
I am currently using Selenium WebDriver to try to read the rows into a List, but can only receive 22 rows at any given time. Here is what I have tried:
IWebElement tableElement = Driver.driverInstance.FindElement(By.XPath(*tableXPath*));
var rowList = tableElement.FindElements(By.TagName("tr"));
Console.WriteLine("Table rows: " + rowList.Count());
Is there any way by which I can read the entire table(450 rows) into a data structure, so I can do in-memory processing on them?
This is how the HTML portion looks like, through FireBug:
<table id="ctl00_MPH_HyperGrid1ContentTable" class="HyperGrid" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr id="ctl00_MPH_HyperGrid1_10" class="" style="height: 37px;">
<tr id="ctl00_MPH_HyperGrid1_11" class="alt" style="height: 37px;">
<tr id="ctl00_MPH_HyperGrid1_12" class="" style="height: 37px;">
<td class="checkable">
<td class="">AntelopeEB</td>
<td class="">CA - Antelope EB</td>
<td class="">SmarterMail</td>
<td class="">User</td>
<td class="">
<td class="loadingCell" colspan="5" style="display:none;">Loading...</td>
</tr>
<tr id="ctl00_MPH_HyperGrid1_13" class="alt" style="height: 37px;">
<tr id="ctl00_MPH_HyperGrid1_14" class="" style="height: 37px;">
"At any given point, the number of rows loaded are not more than 22"
That means only 22 rows can be accessed by selenium and stored into the LIST datastructure as the browser has only 22 rows. You need to trigger an event through SELENIUM to make the remaining data to appear in the browser, so that selenium can retrieve the next 22 tags. You've mentioned that you can access more rows by scrolling, include a function that can trigger the scrolling event. Make use of Thread.sleep(millisecs); if page content loading is slow. Let me know if you need more info, please share the code too
I'm writing test cases for a webpage that has a table as the main focus of the page. I'm checking that the page contains a new entry via the Page Should Contain fucntion, I then want to find the xpath for this element. I am doing this because each entry is added to the end of the table so I cannot set a static xpath for it because I don't know how many entries will be added between each run of this particular test.
Thanks in advance.
<table class="fw-table">
<thead>
<tr class="affix-header">
<th class="columnwidth-description">Email</th>
<th>Account</th>
<th>Signed up</th>
<th>Number of invalid attempts</th>
</tr>
</thead>
<tbody>
<tr>
<td>devtest</td>
<td>Account</td>
<td>true</td>
<td>0</td>
</tr>
<tr>
<td>test#testing.com</td>
<td>Account</td>
<td>true</td>
<td>1</td>
</tr>
search for: xpath=//body/div[2]/div/div[2]/div[2]/div/div/table/tbody/tr[1]/td[1]
If you need to verify that last table row contains certain text you can use the Page Should Contain Element keyword and provide an xpath locator which will find the last table row containing your text:
Page Should Contain Element xpath=//table[#class="fw-table"]//tr[last()]//td[contains(text(),'bottomline')]
What is the code to be used to click the "a" element by looking first for the "td" that contains certain texts?
<table>
<tbody>
<tr>
<td><a class="link">link</a></td>
<td>1st</td>
</tr>
<tr>
<td><a class="link">link</a></td>
<td>2nd</td>
</tr>
</tbody>
</table>
I used this code but it doesn't work.
driver.findElement(By.xpath("//td[contains(text(), '1st')]/following-sibling::a[#class='link']")).click();
Try this xPath //td[contains(text(), '1st')]/../td/a[#class='link'].
Your mistake comes from the axis "following-sibling". The tag a is not a sibling of the td tag. I use .. to go up, then select td again and then a.
I tested the xPath using http://www.xpathtester.com/xpath. It works. Beware that every browser comes with their own xPath implementation, therefore it could behave a bit differently.
However, as there is nothing exotic in the xPath expression, I don't expect any problem.