jsoup to get a particular element in Tablw - java

I have html page that extract the information from:
table class="students">
<tbody>
<tr class="rz" style="color:red;" onclick="location.href='//andy.pvt.com';">
<td>
<a title="Display Andy website,Andy" href="//andy.pvt.com">15</a>
</td>
<td>Andy jr</td>
<td align="right">44.31</td>
<td align="right">23.79</td>
<td align="right">57</td>
<td align="right">1,164,700</td>
<td align="right">0.12</td>
<td align="center">
<td align="left">0.99</td>
<td align="right">
</tr>
=
I want to get Andy, 15 andy.pvt.lom.
I am able to extract this table using doc.select(table).get
I am not able to extract the information I am looking.
how to get the "tables.select("xxxx");"
can you please help me with the xxx what I am missing?

You state:
I tried ; tables = doc.select("table").get(0); than tables.select("a title).
You want something more along the lines of
tables.select("a[href]").attr("href"); // to get your String
and
tables.select("a[href]").text(); // to get your number
e.g.,
Elements tables = doc.select("table");
String hrefAttr = tables.select("a[href]").attr("href");
System.out.println("href attribute: " + hrefAttr);
String number = tables.select("a[href]").text();
System.out.println("number: " + number);

Related

Best way to verify text in a table is between 2 specified items - Selenium - Java

I have the following table and I am wanting to verify that "Vendor Assignment Expired" is in between "Vendor Accepted Assignment" and "Vendor Declined Assignment". What would be the best way to go about doing this?
<table cellspacing="0" cellpadding="0" border="0" style="table-layout: fixed; width: 100%; visibility: inherit;" id="x:1011327536.4:mkr:dataTbl.hdn" mkr="dataTbl.hdn">
<tbody id="x:1011327536.12:mkr:rows:nw:1" class="ig_ListItem igg_ListItem" nw="1" mkr="rows">
<tr id="x:1011327536.13:adr:0:tag:" tag="" adr="0" type="row">
<td class="grdCell" style="width:46%;" idx="0" adr="0" type="cell"> Update Disclosure Date </td>
</tr>
<!--[600001]-->
<tr id="x:1011327536.13:adr:1:tag:" class="ig_ListAlt igg_ListAlt" tag="" adr="1" type="row">
<td class="grdCell" idx="0" adr="0" type="cell"> Vendor Accepted Assignment </td>
</tr>
<!--[101020]-->
<tr id="x:1011327536.13:adr:2:tag:" tag="" adr="2" type="row">
<td class="grdCell" idx="0" adr="0" type="cell"> Vendor Assignment Expired </td>
</tr>
<!--[900101]-->
<tr id="x:1011327536.13:adr:3:tag:" class="ig_ListAlt igg_ListAlt" tag="" adr="3" type="row">
<td class="grdCell" idx="0" adr="0" type="cell"> Vendor Declined Assignment </td>
</tr>
<!--[900102]-->
<tr id="x:1011327536.13:adr:4:tag:" tag="" adr="4" type="row">
<td class="grdCell" idx="0" adr="0" type="cell"> Conditionally Declined </td>
</tr>
I know I can loop through each item like this:
List<WebElement> row = getDriver().findElements(By.cssSelector(".igg_ListItem > tr > td:nth-child(1)"));
for(WebElement el : row)
{
el.getText();
}
I'm just not sure how to capture the values to perform the assertion that the text displays between the other 2.
You can try this -
List<WebElement> row = getDriver().findElements(By.cssSelector(".igg_ListItem > tr > td:nth-child(1)"));
for (int i =0; i<row.size();i++) {
String status = row.get(i).getText();
if (status.equalsIgnoreCase("Vendor Assignment Expired")) {
if (row.get(i-1).getText().equalsIgnoreCase("Vendor Accepted Assignment")
&& row.get(i+1).getText().equalsIgnoreCase("Vendor Declined Assignment"))
System.out.println("Yes it between those values");
break;
}
}
Let me know if it works! Cheers!
You can use something like below:
Get next column Text and assert it with expected value.
String declinedAssignment = driver.findElement(By.xpath("//tr[td[text()=/"Vendor Assignment Expired/"]]//following-sibling::tr/td")).getText();
Get previous column Text and assert it with expected value.
String acceptedAssignment = driver.findElement(By.xpath("//tr[td[text()=/"Vendor Assignment Expired/"]]//preceding-sibling::tr/td")).getText();

Thymeleaf : Getting value from a map while iterating through a list

I'm trying to get the value from a map while iterating through a list. searchResult contains a list of Book objects and each has a list of categories.
categoryFacetCounts is a map that contains counts for each category value. The map has values for all the possible categories (I debugged and checked).
Below is my solution which prints null as the output of the map:
<table class="table table-striped" id="watchlist-table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Categories</th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${searchResult}">
<td th:text="${book.name}"></td>
<td th:text="${book.description}"></td>
<td>
<span th:each="c : ${book.categories}">
<span th:text="${c} + '(' + ${categoryFacetCounts[__${c}__]} + ') '"></span>
</span>
</td>
</tr>
</tbody>
</table>
Use the get method of the map, categoryFacetCounts
<span th:each="c : ${book.categories}">
<span th:text="${c} + '(' + ${categoryFacetCounts.get(c)} + ') '"></span>
</span>
Hope this helps.

Parsing an HTML table in Android studio

I am trying to process a large amount of data for a research project. I have one html file loaded trough Jsoup, but the problem is that the table I need to evaluate does not have an Id or CLASS. I have searched stack, but I don't seem to find an answer as to how I can reach each <tr> and get the information out of its <td>'s.
<table>
<tr>
<td align="center">inf1</td>
<td align="center">date</td>
<tdalign="center">time</td>
<td align="center">group</td>
<td align="center">name</td>
<td align="center">---</td>
<td align="center">room</td>
<td align="center">---</td>
<td align="center">---</td>
<td> </td>
<tdalign="center">reason</td>
<td align="center"> </td>
</tr>
</table>
(The empty <td>'s and the "---" are just for displaying purposes in this table and don't have any value for my project)
I need to sort each <tr> (structured in the same way) by group and inf1 with the other data linked to them in order to use the data in an android Studio project where they will be displayed differently.
Thank you in advance for help:)
You can use Jsoup CSS selectors and a custom class that implements Comparable to keep the records. Something like this:
String html = ""
+"<table>"
+" <tr>"
+" <td align=\"center\">inf1</td>"
+" <td align=\"center\">date</td>"
+" <td align=\"center\">time</td>"
+" <td align=\"center\">group1</td>"
+" </tr> "
+"</table>"
+"<table>"
+" <tr>"
+" <td align=\"center\">inf1</td>"
+" <td align=\"center\">date</td>"
+" <td align=\"center\">time</td>"
+" <td align=\"center\">group0</td>"
+" </tr> "
+"</table>"
+"<table>"
+" <tr>"
+" <td align=\"center\">inf2</td>"
+" <td align=\"center\">date</td>"
+" <td align=\"center\">time</td>"
+" <td align=\"center\">group0</td>"
+" </tr> "
+"</table>"
;
Document doc = Jsoup.parse(html);
class TableRecord implements Comparable<TableRecord>{
public String inf = "";
public String grp = "";
#Override
public int compareTo(TableRecord arg0) {
int cmpGrp = arg0.grp.compareTo(this.grp);
if (cmpGrp==0){
return arg0.inf.compareTo(this.inf);
}
return cmpGrp;
}
#Override
public String toString(){
return "grp="+grp+":inf="+inf;
}
}
List<TableRecord> tableRecords = new ArrayList<>();
Elements trs = doc.select("table tr");
for (Element tr : trs){
Elements tds = tr.select("td");
TableRecord tableRecord = new TableRecord();
tableRecord.inf = tds.get(0).text();
tableRecord.grp = tds.get(3).text();
tableRecords.add(tableRecord);
}
Collections.sort(tableRecords);
for (TableRecord tableRecord:tableRecords){
System.out.println(tableRecord);
}

HREF + TEXT with Jsoup

I've the following HTML Page:
</div><div id="page_content_list01" class="grid_12">
<h2><strong class="floatleft">TEXT1</strong></h2><br>
<table>
<tbody>
<tr>
<th class="no_width">
<p class="floatleft">Attachments:</p>
</th>
<td class="link_azure">
<a target="_blank" href="http://www.example.com">TEXT2</a><br/>
</td>
</tr>
</tbody>
</table><h2><strong class="floatleft">TEXT3</strong></h2><br>
<table>
<tbody>
<tr>
<th class="no_width">
<p class="floatleft">Atachments:</p>
</th>
<td class="link_azure">
<a target="_blank" href="http://www.example2.com">TEXT4</a><br/>
</td>
</tr>
</tbody>
</table><h2><strong class="floatleft">TEXT5</strong></h2><br>
<table>
<tbody>
<tr>
Actually I'm doing:
Elements rows = document.select("div#page_content_list01");
Now I to select "TEXT" and link. I wanna to make clickable link, so I'm using:
for (Element eleme : rows) {
Elements elements = eleme.select("a");
for (Element elem : elementi) {
String url = elem.attr("href");
String title = elem.text();
}
}
and I'm getting:
url = "http://www.example.com";
title = "TEXT2";
and it's ok, but in this way I can't read "TEXT1" and "TEXT3".
Can someone help me please?
I think you need to work on the selecors. First, your primary selector
Elements rows = document.select("div#page_content_list01");
will return with a list of ONE element only, since you actually select the div, not the tables or table rows. I would instead do this to get all relevant info:
Elements tables = document.select("div#page_content_list01>table");
for (Element table : tables){
Element h2 = table.previousElementSibling();
String titleStr = h2.text();
Element a = table.select("a").first();
String linkStr = a.attr("href");
}
Note that the Text in the h2 elements is on the same level as the table, not inside a common div. This is why I use the previous sibling notation. Also note that I wrote this out of my head and it is untested. You should get the idea though.

How to get an old ID and insert into a new text field

I am working on a scenario, where I create a new user. Since, I will be creating tons of new users for regression, I am assigning a time-stamp to distinguish each user id. For instance, ABC_2015020615215, ABC_2015020615222, etc.
The code for creating and sending a user with the time-stamp:
public static String now = "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
// user
public static String user_id = PropertyLoader.loadProperty("USER_ID") + now;
Next scenario: I need to grab the last user added, and insert into a new text field, on a different page. (remaining on the same driver session).
This code creates a new user:
//enter user id in the text field and add a new user
driver.findElement(By.name(PvtConstants.ADD_USER_ID_TEXT_FIELD)).sendKeys(user_id);
This is a new text field, where I need to insert the user that was added in the previous step. I am not able to figure that out. Thanks in advance for the help!
driver.findElement(By.id(PvtConstants.READ_USER_ADVANCED_FILTER_USER_SEARCH_FIELD));
HTML:
<table class="table smallInput dataTable" id="dataTableAdvertisers" ">
<thead>
<tr role="row" style="height: 0px;">
<th class="sorting_asc" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
</tr>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
<tr class="odd">
<td class="">
Advertiser_20150204130817</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td>
</tr><tr class="even">
<td class="">
Advertiser_20150204130923</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td><td class="">---</td>
</tr><tr class="odd">
</tbody>
</table>
I will use a Xpath and with passing the user_id as parameter.
Note: this xpath matches the any a tag with the text supplied by the user_id variable which in your case is the lastly instantiated variable with timestamp.
By byXpath = By.xpath("//a[.='" + user_id + "']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byXpath));
String newUser = myDynamicElement.getText().trim();
//send newUser this to the search field

Categories

Resources