I am using the PageObject gems to test Salesforce which is chock full of tables within tables. I wanted to know if anyone has used a specific technique to access cells within nested tables (see example below).
I want to access THE LINK inside the cell with the cell labeled id="desired_item"
Thanks in advance.
<table id="bodyTable" class="outer">
<tbody>
<tr>
<td id="blah">
<div>
<table class="detailList">
<tbody>
<td>
<tr>
<td id="desired_item">
<a>Click_Me_Link</a>
</td>
</tr>
</td>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
Since the parent cell of the link has an ID, you can easily find that cell. From there, you simply get the first link in the cell.
page.cell_element(id: 'desired_item').link_element
Related
I have html page like this:
<table>
<thead>
<tr>
<td>title</td>
<td>desc</td>
<td>status</td>
</tr>
</thead>
<tbody>
<tr>
<td><label>lorem1</label></td>
<td><label>desc1 lorem</label></td>
<td><label>active</label></td>
<td> Delete </td>
</tr>
<tr>
<td><label>lorem2</label></td>
<td><label>desc2 lorem</label></td>
<td><label>active</label></td>
<td> Delete </td>
</tr>
<tr>
<td><label>lorem3</label></td>
<td><label>desc3 lorem</label></td>
<td><label>deactive</label></td>
<td> Delete </td>
</tr>
</tbody>
</table>
Now I delete record lorem2 from above list (with click on delete link) and after that I want to check lorem2 that deleted shouldn't exist or contain in page.
I write this code but it's not correct:
expect(element(by.css("table")).getText()).not.toBe('lorem2');
You will delete the lorem2 by a locator may be xpath
below for deleting
//tr/td//label[contains(text(),"lorem2")]/following::td/a
below for checking if exist after deletion
//tr/td//label[contains(text(),"lorem2")]
you should parameterize xpath (i.e) the text Lorem2 for other text.
expect(element(by.xpath('//tr/td//label[contains(text(),"lorem2")]
')).isPresent()).toBe(false);
How to get the table rows which is nested in other tables and form tag.
I tried few codes but doesn't seem to work.
I have used the below python code but not able to get anything
def parse(self, response):
t = response.xpath('//table[#class="DataGrid"]/tbody/tr')
for tr_obj in enumerate(t):
print(tr_obj.xpath('td[1]/text()').extract_first())
Below is html code and in this I need to get table which has the class name as gridTable
<html>
<body>
<table></table>
<table>
<tbody>
<tr>
<td>
<span></span>
<script></script>
<form>
<table class="dPage1">
<tbody>
<tr></tr>
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<table class="gridTable">
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Solution
for tr_obj in enumerate(response.xpath('//table[#class="DataGrid"]/tr')):
print(tr_obj.xpath('td[1]/text()').extract_first())
You can choose which tags to follow in xpath by specifying the tag in brackets.
For your example it would be:
//table[#class="gridTable"]/...
It is recommended that you must not use tbody in your XPath statements in the scrapy documentation.
So try without them and/or try to circumvent them by using /*/ or //.
Try something like:
def parse(self, response):
# Get a Selector list for all rows
sel_rows = response.xpath('//table[#class="DataGrid"]/tr')
# loop over row selectors ...
for sel_row in sel_rows:
print(sel_row.xpath('td[1]/text()').extract_first())
Consider this code:
#inherits Umbraco.Web.Macros.PartialViewMacroPage
#{
var pages = #CurrentPage.Children;
}
<table style="width:100% !important">
<thead>
<tr>
<td>Subbasin Name</td>
<td style="width: 15% !important">Hydrologic Unit Code (HUC)</td>
<td style="width: 25% !important">TMDL Status</td>
<td style="width: 25% !important">Implementation Plan Status</td>
<td style="width: 15% !important">Five Year Review Status</td>
</tr>
</thead>
<tbody>
#foreach (var page in #pages){
<tr>
<td>#page.pageTitle <br />#page.watershedName</td>
<td>#page.hydrologicUnitCode</td>
<td>#page.tmdlStatus</td>
<td>#page.implementationPlanStatus</td>
<td>#page.fiveYearReviewStatus</td>
</tr>
}
</tbody>
</table>
Problem: The table draws and populates as desired but for one thing...
#page.pageTitle
The URL of each of the child pages isn't right.. each row of the table has the right Title, and the other columns are correct, but the anchor is being formed with the URL of the CurrentPage rather than each Child's .Url. I assume I am doing something wrong, but I cannot see why this isn't working.
Any suggestions?
Thanks!
Found my problem - case sensitivity (duh).
Changing:
#page.pageTitle
to:
#page.pageTitle
fixes the problem.
I have a partial view like this:
#model List<user>
#foreach (var user in Model)
{
<tr>
<td>#user.name</td>
<td>...</td>
</tr>
}
And get an error like this:
Validation (HTML5): Element 'tr' cannot be nested within element 'tr'.
It's annoying me more than it should, but I want to get rid of it. Installing Web Standards Update didn't help. Any ideas?
Edit
This is the main view:
<table>
<thead>
<tr>
<th>#i18n.name</th>
<th>...</th>
</tr>
</thead>
<tbody id="results">
#Html.Partial("list_rows", #Model.users)
</tbody>
</table>
This is the generated HTML:
<table>
<thead>
<tr>
<th>naam</th>
<th>...</th>
</tr>
</thead>
<tbody id="results">
<tr>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
Edit Pulling the entire page through the W3C validator gives
This document was successfully checked as HTML5!
This error appears when you open a <tr> element before you loop through your model. So far the code you postet is correct and free of errors.
Just make sure that your code looks something like this:
<table>
#foreach (var user in Model)
{
<tr>
<td>#user.name</td>
<td>...</td>
</tr>
}
</table>
It seems like you already have an open tr tag in which you are trying to add more tr tags. If you already have tr tags in your table, just make sure they are all closed before the loop starts:
<tr>..</tr>
I'm using the Ruby Watir library to do automated testing for a client and I'm having issues with the XPath selector. I think I just need another set of eyes to let me know if I'm just missing something.
Here is the selector I'm using:
puts ie.cell(:xpath, "//img[#src='3.jpg']/../").text
For this set of tables, it works as expected and prints "Third Image":
<table>
<tr>
<td><img src="1.jpg">First Image</td>
</tr>
</table>
<table>
<tr>
<td><img src="2.jpg">Second Image</td>
</tr>
</table>
<table>
<tr>
<td><img src="3.jpg">Third Image</td>
</tr>
</table>
But is is breaking when I remove the second table:
<table>
<tr>
<td><img src="1.jpg">First Image</td>
</tr>
</table>
<table>
<tr>
<td><img src="3.jpg">Third Image</td>
</tr>
</table>
Using the puts code above I get this error on the second example:
Watir::Exception::UnknownObjectException: Unable to locate element, using :xpath, "//img[#src='3.jpg']/../"
I reproduced the problem, and restarting the browser (IE6) fixed it for me.
For current versions of Watir the better way to do this would be
browser.img(:src => '3.jpg').parent.text