Java - reading data from website [duplicate] - java

This question already has answers here:
Parse Web Site HTML with JAVA [duplicate]
(3 answers)
Closed 7 years ago.
I know how to read data from xml file, but i can't find any examples of reading data from website (HTML). All examples include xml files. I need do it with DOM parser or XPath. Html code:
<table class="tabela">
<thead>
....
</thead>
<tbody>
<tr >
<td>...</td>
<td class="al">WIG20</a></td>
<td class="ar">2242.42</td>
<td class="ar">2551.47</td>
<td class="ar">2522,37</td>
<td class="ar">2505,41</td>
<td class="ar">2524,83</td>
<td class="ar">2516,76</td>
<td class="ar">
<span class="r_dn">-0,32</span>
</td>
<td class="ar">564,34</td>
<td class="ar">5</td>
<td class="ar">14</td>
<td class="ar">1</td>
<td class="ar">17:15:00</td>
</tr>
<tr >
...
</tr>
</tbody>
There is any possible to read all lines from this table ?

JSoup library is designed specifically for that purpose.

Related

Diffulties in identifying web page element in salesforce application

Currently working on the automation of salesforce application.
I need a quick help to check the below code, i am unable to identify the element using java container. here is the HTML source for the page.
<div class="pbBody">
<span id="j_id0:j_id1:j_id5:matrixGrid">
<div id="j_id0:j_id1:j_id5:j_id7">
<div class="pbSubsection">
<table class="detailList" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<tr>
<th class="labelCol vfLabelColTextWrap last " scope="row">
<td class="dataCol last ">EUR</td>
<th class="labelCol vfLabelColTextWrap last " scope="row">
<td class="dataCol last ">
<span id="j_id0:j_id1:j_id5:j_id7:j_id11:theDrPriceMatrix">Germany</span>
</td>
</tr>
</tbody>
</table>
JAVA Container is written here to identify element in tag
WebElement w5=driver.findElement(By.cssSelector("table.detailList"));
System.out.println(w5);
WebElement w6=w5.findElement(By.xpath("/html/body/form/span/div/div/div/div/div[1]/span/div/div/table/tbody/tr[2]"));
WebElement w7=w6.findElement(By.cssSelector("td.dataCol"));
List<WebElement> l3=w7.findElements(By.tagName("span"));
for (int i = 0; i <l3.size();i++)
{
System.out.println(l3.get(i).getText());
}
l3.get(0).getText();
actually the issue is I am not able to write anything for in the container

How to click a button from a table where all classes have the same name

<tbody>
<tr class="b">
<td class="t_l">betmatrix</td>
<td class="t_l">
<td class="t_r">dsdfsf</td>
<td class="t_r">NONE</td>
<td class="t_r">ALL</td>
<td class="t_r">NONE</td>
<td class="t_r">ALL</td>
<td class="t_r">ALL</td>
<td class="t_r">NONE</td>
<td class="t_r">ALL</td>
<td class="t_r">Not required</td>
<td class="t_r">1.11</td>
<td class="t_r">All User</td>
<td class="t_r">0.00</td>
<td class="t_r">2014-09-05</td>
<td class="t_r">2014-09-15</td>
<td class="t_r">2014-09-05</td>
<td class="t_r">1-admin</td>
<td class="t_r">
<td class="t_r">0.00</td>
<td class="t_r">0.00</td>
<td class="t_r">
<td class="t_r">
<a onclick="showBonusModificationLogs(53599)" href="#">History</a>
</td>
</tr>
<tr class="a">
<tr class="b">
<tr class="a">
</tbody>
This is the table structure that I have, and there is no way I can search only for specific items in the table.
How can I find an element in a row and then click on the button at the end of the row if it is found?
In this example I would need to search for the word: dsdfsf
It was easier for other tables because the classnames were different. But the same method will not work here.
As #alecxe pointed out, you can use XPath. There are probably hundred different ways of doing this. But to match your requirements I would use: //tr[td[text()='dsdfsf']]/td/a. This says: any row that has a column with the text 'dsdfsf', locate a column with an anchor tag.
You can find element By.xpath:
WebElement element = driver.findElement(By.xpath('//table//tr[td[#class="t_r"] and text() = "dsdfsf"]/td[last()]'));
element.click();
The xpath would search in every tr inside the table for the td with class t_r and text dsdfsf, then would get the last td inside the row.

add a serial number while performing iteration in thymeleaf

I have a senario in which i must perform iteration on a list and display result as a grid.Also include serial number in it. Now what I did for serial number is given below
<div th:with="i=0">
<tr th:each="mydate:${data.Listdata}" >
<div th:with="i=${i+1}">
<td th:text="${i}">
</div>
//other codes
</tr>
</div>
but only 1 appears in all serial number.Can anyone help me with this?
You can use the index of the iteration status:
<tr th:each="item,iterator : ${items}">
<td th:text="${iterator.index}"></td>
</tr>
You can use the iterationStatus to begin with 1 instead of 0.
<tr th:each="item,iterationStatus : ${items}">
<td th:text=${iterationStatus.count}></td>
</tr>
This gives the table Sno. Starting with 1.
To get the index:
<tr th:each="item: ${items}">
<td th:text="${iterator.indexOf(item)}"></td>
</tr>
To get a numbering increment index by one.
<tr th:each="item: ${items}">
<td th:text="${iterator.indexOf(item) + 1}"></td>
</tr>

Alternative of element.children in jsoup

Alright I am having trouble with finding an equivalent of Element.children() because I have an Elements object...
What I am trying to do is download an html file (well I have done that...) and identify a single table row (, I've done that by using doc.getElementsByClass("emphasizedRowColor"); because the row I want has that emphasizedRowColor class and no other elements do). I just don't understand how to isolate the one Element in my Elements object RWTableRow.
Html:
<tr class="rwOdd emphasizedRowColor">
<td class="jewel" style="">
<div class="teamJewel" style="background-position: 0px -336px;margin: 0 0 2px 2px;"></div>
</td>
<td class="left" style=""> Detroit</td>
<td style="">18</td>
<td style="">9</td>
<td style="">5</td>
<td style="">4</td>
<td class="narrowStatsColumn cSrt" style="">22</td>
<td class="narrowStatsColumn" style="">9</td>
<td style="">45</td>
<td style="">48</td>
<td style="">3-2-4</td>
<td style="">6-3-0</td>
<td style="">3-3-4</td>
</tr>
I can figure out what to do once I actually get the table as an Element but oh boy I think I just need a fresh set of eyes to figure out what I'm doing...
Java:
Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
Elements RWTableRow = doc.getElementsByClass("emphasizedRowColor");
As you can see, I'm in quite the pickle...
Elements is a standard java.util.List, you can simply call
Element e = RWTableRow.get(0);
And there you have it.

Java - Calling onclick method on webpage

<tr id="data_11">
<td colspan="7" class="row">
<table class="table-inner">
<tbody>
<tr>
<td class="col-companyname" onclick="getDetails('11', 'ARBN=BN5321177&CompName=A+ACTIVE+PLUMBING&CompStat=DRGD&Type=BUSN&BusIDType=Number&BusID=65057196&=')" style="cursor:pointer;">A ACTIVE PLUMBING</td>
<td class="col-companytype">Business Name</td>
<td class="col-acn"> </td>
<td class="col-abn"> </td>
<td class="col-arbn">BN5321177</td>
<td class="col-state"> </td>
<td class="col-docimage"> </td>
</tr>
<tr id="row_11" style="display:none;">
<td colspan="7">
<div id="div_11"></div></td>
</tr>
</tbody>
</table></td>
</tr>
I need to call the onclick method getDetails with the above given parameters and get the result. I tried doing this with jsoup but i couldnt get it to work.
Thanks in advance
Simple Answer : You can't call a javascript function from Jsoup. Jsoup is primarily meant for extracting and manipulating html data.
If you are interested in knowing the response to the request made in getDetails, then you can simulate the same request in Jsoup and get the response. But, If its a business logic in javascript without any server interaction which changes the DOM then you can kiss-off your idea.

Categories