I need to do assertion with the total of table and to do this when I am writing XPath for total in image table its not working for all the scenario. I know the reason because the table is dynamic so sometimes its have only 1 Image and sometime its have 5 Image so I am having difficulty to wrote x-path which works for all the scenario.
For the attached image I have written : enter image description here
WebElement totalElement = driver.findElement(By.xpath("//*[#id="image_table"]/tbody/tr[5]/td[4]"));
its running fine but when the table size changes. it failed.
for reference the webpage design is as below,
***<table class="table table-bordered table-striped table-hover display" id="image_table">
<tr>
<td colspan="2" class="text-right">Total</td>
<td class="text-center">1,650</td>
<td class="text-center">19,936</td>
<td class="text-center">21,586</td> (trying to write xpath for this total)
</tr>***
Try css:
#image_table>tr>td:last-of-type
It always defines the last element.
Update based on comment. This css selector is more robust:
#image_table tr:last-of-type>td:last-of-type
//td[contains(string(),"Total")]/following-sibling::td[last()]
find td that contains the text total and find the last following td
Related
As described in this and this answers, we can use HTML tags in Markdown to create tables with cells spanning multiple rows or columns. Suppose I want to make the following table (rendered by Visual Studio Code with extension "Markdown All in One"):
Desired Effect
But the problem for the second answer is that the rowspan/colspan of the first column/row will be rendered with a additional column/row in the front, as in the following example (does not work on StackOverflow, but works in VSCode; a work-around is to add an empty column/row, as suggested by the first comment in that answer, but this is still not perfect):
||Letter|Typesetting|Result|
|-|-|-|-|
|<td rowspan=4>a |Normal|a
||Italic|*a*|
||Bold|**a**|
||Math|$a$
Result:
Table constructed using method in Answer 2
Then we can use the HTML tags, as described by the first answer:
<table>
<thead>
<tr> <th>Letter <th>Typesetting <th>Result
</thead>
<tbody>
<tr> <td rowspan=4>a <td>Normal <td>a
<tr> <td>Italic <td>*a*
<tr> <td>Bold <td>**a**
<tr> <td>Math <td>$a$
</tbody>
</table>
But then Markdown formatting will be lost:
Table constructed using method in Answer 1
Is there any way to overcome this problem, i.e. using HTML tags to achieve rowspan/colspan without loss of Markdown text formatting?
using Objective-C HTMLReader for my first (simple, I think) HTML scraping task. But there's little documentation with it, and after a lot of experimentation, can't quite get what I need.
I'm scraping an old HTML page whose largest feature is one table with three columns and many rows. Here's a sample of the table with one row:
<table border="1" cellspacing="2" cellpadding="6" bordercolor="#000000" bgcolor="#999999" style="margin-top:50px;width:100%;">
<tr height=30>
<td bgcolor="#34003C" align="left" valign="middle" background="background.gif"><span class="cls_TableHeader">Bands</span></td>
<td bgcolor="#34003C" align="left" valign="middle" background="background.gif"><span class="cls_TableHeader">Style</span></td>
<td bgcolor="#34003C" align="left" valign="middle" background="background.gif"><span class="cls_TableHeader">Country</span></td>
</tr>
<tr>
<td class="cls_tdDisco0" align="left" valign="middle">
<strong>THE BEATLES</strong>
</td>
<td class="cls_tdDisco0" align="left" valign="middle">
<span class="cls_DiscoText">Rock</span></td>
<td class="cls_tdDisco0" align="left" valign="middle"><span class="cls_DiscoText">England</span></td>
</tr>
there are, of course, many rows.
What I'm trying to accomplish:
I need to search for the td that contains "THE BEATLES", and extract the href attached to it (of course, even when it's contained in the middle of a lot of other rows)
What I've tried:
I can get the table itself with
HTMLDocument *home = [HTMLDocument documentWithData:data contentTypeHeader:nil];
HTMLElement *table = [home firstNodeMatchingSelector:#"TABLE"];
HTMLNode *theActualTable =[table childAtIndex:1];
but I can't really use the method "nodesMatchingSelector" to search rows since what I'm looking for isn't a selector. I've tried getting the rows (via children), but then I'm looking at iterating through each row's children of children until I drill to the tag that contains THE BEATLES and then using that index to get the a tag attached to that? It seems that there should be a much easier way to do this with HTMLReader. I feel like I'm missing something simple.
Thanks in advance!
Here is some psuedo code that might work for you:
Use nodesMatchingSelector to get all the tr in the table
Then loop through all the tr and get the first td of each tr
Then use nodesMatchingSelector again to get the strong tag
Then use node.textContent to get the text content of the strong tag
https://github.com/nolanw/HTMLReader has an example in the readme that shows using the textContent method
feel free to post follow up questions as comments if any of this doesn't make sense
I am trying to extract some info from the table below into Excel using VBL without any success. The values which I need do not seem to have any element ID, tag name or class name assigned to it. I'm after the Fuel Usage value(89218) and the time value in the same row (01:15). Can anyone point me in the right direction on how to scrape values from a table, or how to extract data from specific TR, TD.
HTML source of the table:
<h3>Airbus A300-600-PW4158 Fuel Planner</h3>
<p>London to Chicago EGKK-KORD (3441 NM)<br /></p>
<h2>Total Fuel: 101901 POUNDS</h2>
<table width="100%" border=1>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:left;">Fuel</td>
<th style="text-align:left;">Time</th>
</tr>
<tr>
<td>Fuel Usage</td>
<td>89218</td>
<td>08:47</td>
</tr>
<tr>
<td>Reserve Fuel</td>
<td>12682</td>
<td>01:15</td>
</tr>
<tr>
<td>Fuel on Board</td>
<td>101901</td>
<td>10:02</td>
</tr>
</table>
much appreciated.
CSS Selectors:
Without seeing more of the HTML you can use the following CSS selectors selectors for the snippet shown:
tr td:nth-child(2)
tr td:nth-child(3)
With CSS selectors this will bring back nodeLists of all 2 or 3 child tds with a tr.
For example:
You can access individual items from a nodeList by index.
VBA:
The syntax in vba overall will be something like:
.document.querySelectorAll("tr td:nth-child(2)")(0).innerText
or possibly
.document.querySelectorAll("tr td:nth-child(2)").Item(0).innerText
The 0 is hypothetical. You would need to inspect your full HTML to ascertain the correct index to use.
The .document innerHTML can be populated from the .responseText using IE, for example, to navigate to the page.
I have to get the Text from the td elements from a table in html which looks like this:
<table id="gvrslt" >
<tbody><tr style="font-size:10pt;">
<th scope="col">Sem</th><th scope="col" style="font-size:X-Small;">Total Obtained Marks</th><th scope="col" style="font-size:X-Small;">Max Total Marks</th><th scope="col">Result</th>
</tr>
<tr>
<td align="center">VI</td>
<td align="center">458</td>
<td align="center">550</td>
<td align="center">PASSED</td>
</tr>
</tbody></table>
I want to grab the 458 from the table which has more such td elements.The problem is that before getting to the Results' page and getting the above HTML, I have to enter some credentials and then a Result page is shown with Right click disabled. Now I can get the source of the Results' page via driver.page_source but when I try to find the table elements via webdriver, it searches the page where I entered the credentials and not the actual results' page. Is there a way to search the driver.page_source for table and td elements
Here is my code:
html=driver.page_source
soup = BeautifulSoup(html)
table=soup.find_all('table',id='gvrslt')
print(table)
If you want to get the text directly you can use a css locator to get to the 2nd td directly instead of using the table.
table[id='gvrslt'] td:nth-of-type(2)
nth-of-type gets you the 2nd td element
Try using Xpath in this case:
//table[#id='gvrslt']//td[index]
with your index of td
I'm not familiar with selenium using python. What you try is find the value using xpath.
In C# below is the code. See if it can hep you in any way possible.
IWebElement tdCell = driver.FindElement(By.XPath("//table[#id='']/tbody/tr[2]/td[2]"));
string valueOfTd = tdCell.Text;
In Firefox/Chrome, this table displays correctly
<table border="1" style="width: 400px; height: 400px">
<tr>
<td colspan="2" rowspan="3" style="height:300px">a</td>
<td rowspan="2" style="height: 200px">b</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2" style="height: 200px">c</td>
</tr>
<tr>
<td style="height: 100px">d</td>
<td style="height: 100px">e</td>
</tr>
</table>
like this - with cells b and c being 200px each.
Fiddle
but in Internet Explorer 11 it looks like this - with cell b being stretched by A, and cells d and e being stretched by c
(unfortunately I don't have enough reputation points to post pictures)
I've tried putting fixed sized divs inside, using CSS instead of HTML, etc, but not having any luck. Amy I missing something simple here?
The table markup violates the HTML table model as described in HTML5 PR. You can see this if you put <!doctype html><title>test</title> before your code and then test it at http://validator.w3.org – the validator reports the error “Row 2 of a row group established by a tbody element has no cells beginning on it.” Your second row is just <tr></tr>, so it has no cells of its own. Since it is invalid, there is no “correct rendering”, and in fact browsers display the table differently.
The solutions depend on what you are using the table element for. If it is tabular data and needs to be accessible as a table, you need to re-analyze the data and find a valid way of describing it in HTML. If it is just for layout, use some other layout technique. It is common to recommend CSS for everything, but you can in fact use table layout, too, you just need nested tables. E.g., set up a 1 × 2 table and put a, d, and e in a simple table inside the first cell, b and c inside the second cell, and set dimensions suitably.
You are missing nothing. Tables are just rendered different in IE. My advice is that you use regular divs without tables, and style them with CSS. Tables are just not going to be rendered the same cross browser.
Hope this helped!