I have a table like in this fiddle. I need to find data related to the row which contains given text.
For example, by providing 1707, I need to get all data in table row which contains 1707. So output should be as below.
Tuesday 2014-08-05 1707 33 43 47 52 image text
Currently I'm accessing data on html page as below.
Document doc;
try {
doc = Jsoup
.connect("url here").timeout(300000).userAgent("Mozilla").get();
Element table = doc.select("table#customers").first();
if (table != null) {
Iterator<Element> iterator = table.select("td").iterator();
while (iterator.hasNext()) {
System.out.println("Day : " + iterator.next().text());
System.out.println("Date : " + iterator.next().text());
System.out.println("Draw : " + iterator.next().text());
System.out.println("No1 : " + iterator.next().text());
System.out.println("No2 : " + iterator.next().text());
System.out.println("No3 : " + iterator.next().text());
System.out.println("No4 : " + iterator.next().text());
System.out.println("Symbol : " + iterator.next().text());
System.out.println("Non : " + iterator.next().text());
}
} else {
System.out
.println("No results were found according to search criteria.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
The above code return all data on table. But I need to get data related to given text.
How could I achieve this?
As shown in jsoup documentation you can use the pseudo-selector :contains(text):
table.select("tr:contains(1707) td")
You can try it here
Related
I am getting sometimes problems with creating a list of names of the attached files in a NotesDocument. The custom message looks as followed:
AttachmentDominoDAO - General problem during reading attachment from
entry 39E411CEC4AD22F3C1258821003399EF in database mail.nsf.
fileObject.getName() returns null. Files found in document
[contract.pdf]
Here is the method that I am calling:
private Attachment loadFromEntry(ViewEntry entry) {
utils.printToConsole(this.getClass().getSimpleName().toString() + " - loadFromEntry(...) unid=" + entry.getUniversalID());
Attachment attachment = new Attachment();
try{
attachment.setUnid(entry.getUniversalID());
Document doc = entry.getDocument();
if (null != doc){
attachment.setCreated(doc.getCreated().toJavaDate());
if(doc.hasItem("$FILE")){
List<String> files = doc.getAttachmentNames();
for (int j = 0; j < files.size(); ++j) {
EmbeddedObject fileObject = doc.getAttachment(files.get(j));
if(null != fileObject.getName()) {
if(null != fileObject.getName()) {
attachment.setFile(fileObject.getName());
} else {
XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", fileName.getName() returns " + fileObject.getName(), Level.SEVERE, null);
}
if(null != fileObject.getName() && !utils.Right(fileObject.getName(),".").isEmpty()) {
attachment.setExtension(utils.Right(fileObject.getName(),"."));
} else {
XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", extension is empty for file " + fileObject.getName(), Level.SEVERE, null);
}
attachment.setSizeHuman(FileUtils.byteCountToDisplaySize(fileObject.getFileSize()));
if(fileObject.getFileSize() > 0) {
attachment.setSize(fileObject.getFileSize());
} else {
XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", fileName.size() returns " + fileObject.getFileSize(), Level.SEVERE, null);
}
if(null != doc.getAuthors() && null != doc.getAuthors().firstElement()) {
attachment.setCreator(doc.getAuthors().firstElement());
} else {
XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", doc.getAuthors().firstElement() returns " + doc.getAuthors().firstElement(), Level.SEVERE, null);
}
String fieldName = "type";
if (doc.hasItem(fieldName)) {
attachment.setType(fieldName);
}
}else {
XspOpenLogUtil.logEvent(null, "AttachmentDominoDAO - General problem during reading attachment from entry " + entry.getUniversalID() + " in database " + entry.getDocument().getParentDatabase().getFileName() + ". fileObject.getName() returns null. Files found in document " + doc.getAttachmentNames().toString(), Level.SEVERE, null);
}
}
}
}
}catch (Exception e) {
XspOpenLogUtil.logEvent(e, "General problem with reading attachment from entry " + entry.getUniversalID() + " in database " + entry.getDocument().getParentDatabase().getFileName(), Level.SEVERE, null);
}
return attachment;
}
One document can only contain one file. When I check the document there is only one attachment and the attachment mentioned in the error message.
Anyone has a suggestion how to fix this issue?
Note: in 99% of the cases the error does not occur.
If getName() returns null for an attachment, try using getSource() instead. As long as it's an actual attachment (as opposed to an OLE embedded object), then getSource() always returns the original file name.
NotesDocument.getAttachment does not find attachments that were created in rich text fields. It only finds attachments that were created directly in the document itself.
We used to call an attachment that is directly acceessed through the document a "V2 attachment" because that's the way things worked in Notes V2 -- over 30 years ago. Objects created through OLE launch properties on the form can also attach objects that are directly in the document instead of inside rich text. Since OLE and V2 are both relics of the deep, dark past, almost all attachments are created inside rich text fields these days.
Attachments that are inside rich text fields are accessed through the getEmbeddedObject method of the RichTextItem class.
I have a hashmap with 2 objects, Parcel and Deliverer. Both have a getName function. I want to loop the hashmap and print there names like (Parcel.getName(), Deliverer.getName()).
for (Map.Entry<Parcel, Deliverer> entry : deliveryList.entrySet()) {
for (Parcel key: entry) {
System.out.println("Package : " + key.getName());
for (Deliverer deliverer: entry) {
System.out.println("- Deliverer : " + deliverer.getName());
continue;
}
}
}
Another way can be using passing BiConsumer to forEach directly
deliveryList.forEach((key, value) -> System.out.println("Package: " + key.getName() + "- Deliver: " + value.getName()))
Not any different from #funkyjelly. An upvote :)
Same thing, just with Java8 collection foreach syntax.
deliveryList.entrySet().forEach( entry ->
System.out.println("Package : " + entry.getKey().getName());
System.out.println("Deliverer: " + entry.getValue().getName());
);
Once you have the map entries you'd only need to invoke getKey() to get Parcel and getValue() to get Deliverer for each entry.
Hence it should be like this :
for (Map.Entry<Parcel, Deliverer> entry : deliveryList.entrySet()) {
System.out.println("Package : " + entry.getKey().getName() +
"- Deliverer : " + entry.getValue().getName());
}
I have a problem in my code when I am creating a table in Java using HTML. This is my code:
for(int station : stations){
String rowcolor = null;
String stationnum = Integer.toString(station);
String lastDate = pollData(station); //CALL GET LAST
String status = determineStatus(station, lastDate); // CALL DETERMINE STATUS
switch(status){
case " ONLINE":
rowcolor = (" <tr bgcolor=\"#5FFF33\">");
break;
case " OFFLINE":
rowcolor = (" <tr bgcolor=\"red\">");
break;
case " DELAYED":
rowcolor = (" <tr bgcolor=\"yellow\">");
break;
}
out.write("<html>" +
"<body>" +
"<table border ='1'>" +
"<tr>" +
"<td>Station Number</td>" +
"<td>Station Name</td>" +
"<td>Status</td>" +
"<td>As of Date</td>" +
"</tr>");
out.write(rowcolor + "<td>");
out.write(stationnum);
out.write("</td><td>");
out.write(stationnname[id]);
out.write("</td><td>");
out.write(status);
out.write("</td><td>");
out.write(lastDate);
out.write("</table>" +
"</body>" +
"</html>");
id++;
out.close();
}
}catch (IOException e) {
System.err.println(e);
}
and this is the output:
When I remove the out.close(); part, the output is this:
As you can see, the image there is a problem in creating the table. Something is not right but I can’t find a way to fix it. Please help me; thanks in advance.
Look at what you're writing to the output buffer and where.
Inside your for loop, you are writing a complete HTML document (ie <html><body>...</body></html>) and an entire table with header row and one data row.
What I assume you want to do is keep writing table rows to the one table. To do so, write the aforementioned tags outside your for loop
out.write("<html><body><table border=\"1\"><thead>" +
"<tr><td>Station Number</td><td>Station Name</td>" +
"<td>Status</td><td>As of Date</td></tr></thead><tbody>");
for(int station : stations) {
// get data, determine rowcolor, etc
out.write(rowcolor + ... + "</tr>");
}
out.write("</tbody></table></body></html>");
out.close();
As Phil said,out.close(); is inside the for loop,you need to change it to outside the for loop,due to if it's inside loop,out will close for the first iterate,and will not work for other records
for(int station : stations){
}
out.close();
I am trying to extract the value of an HTML table element from a website and compare it to a user input value but it seems that the nested loop is not being entered when I run the program. It works with no errors but I am not getting any output from Eclipse, I'm new to Selenium Java and still learning.
See my code below:
String inputString = basePrem;
try {
//Print to console the value of Base Prem
WebElement table = driver.findElement(By.xpath(".//td[text()='Base Premium']/following-sibling::*"));
List<WebElement> allrows = table.findElements(By.tagName("tr"));
List<WebElement> allcols = table.findElements(By.tagName("td"));
for (WebElement row: allrows) {
List<WebElement> Cells = row.findElements(By.tagName("td"));
for (WebElement Cell:Cells) {
if (Cell.getText().contains(basePrem)) {
System.out.print("Base Premium = "+ basePrem + " ");
}
else if (!Cell.getText().contains(basePrem))
{
System.out.print("Base Premium = " + basePrem + " ");
break;
}
}
}
}
catch (Exception e) {
errorMessage = "Value discrepancy";
System.out.println(errorMessage + " - " + e.getMessage());
driver.close();
}
Also, inputString is where I input the value I use for comparison (I use a separate excel file for testing)
Since the control is not going inside the nested loop, I probably have some logical error?
You can rewrite the code as below and then validate whether your inputstring is available in the table. It is not necessary to use nested for loops
Code:
String inputString = basePrem;
WebElement table = driver.findElement(By.xpath(".//table"));
//Extract all the Cell Data Element
List<WebElement> dataElementList=table.findElements(By.xpath(".//td"));
for(WebElement dataElement : dataElementList){
if(dataElement.getText().contains(inputString)){
System.out.print("Base Premium = "+ basePrem + " ");
break;
}
}
I am novice in java and I am using elasticsearch and I am looking a way to put Json's documents in a List string. The things is, I want to read a Json file and then stored each Json's documents in a List String :
Example:
readTheJsonfile = (src/main/resource/file.json)
and then for example:
<
List <String > readTheJsonfile [1] = "{\n" +
" \"user\" : \"stack\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying search\"\n" +
"}"
List <String > readTheJsonfile [2] = "{\n" +
" \"user\" : \"flow\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying Ela\"\n" +
"}"
>