I'm trying to get "title" attribute value and save it in csv file from element below:
<img src="images/i.png" title="Uwagi: łacina, nieczytelne
Data urodzenia: 25.02.1808 r.">
Whole html here.
I've got this attribute value using xpath below (it works):
SelenideElement uwagi = $(By.xpath("//div[#id='table_b_wrapper']//table[#id='table_b']//tbody//tr[1]//img[contains(#title,'Uwagi')]"));
//tr[1] is just a one example from this table. xpath is ok
Then I've tried to put it into my csv file with:
writer.append(uwagi+";"); //using ; as separator
Problem is that this value "Uwagi: łacina, nieczytelne
Data urodzenia: 25.02.1808 r."
It's divided into 2 parts and they are saved as separate cells, like here
I need all this value in one cell (i.e. J1731 and A1732 values should be as 1 cell).
What's strange when I did System.out.println(uwagi.getAttribute("title"));
only 2nd part of attribute value (Data urodzenia: 25.02.1808 r.) was displayed in console.
How can I save this title attribute value as one cell in csv?
Regards
Tomes
Remove new line character from the title, code below replace \n (new line character) with one space as needed per your shared html.
Also in Selenide you can use $x for xpath selectors:
SelenideElement uwagi = $x("//table[#id='table_b']//tr[#role='row'][1]//img[contains(#title,'Uwagi')]");
//using css selector
uwagi = $("#table_b tr[role='row'] img[title^='Uwagi']");
//or even shorter
uwagi = $("#table_b img[title^='Uwagi']");
String uwagiTitle = uwagi.text().replace("\n", " ");
writer.append(uwagiTitle+";");
I've found solution. I've changed:
FileWriter writer = new FileWriter(pathString, Charset.forName("Cp1250"));
to
CsvWriter writer = new CsvWriter(pathString, ';', Charset.forName("Cp1250"));
using also:
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
Based on the info from: link
Then I've changed writer.apend to writer.write.
Other is the same:
...
SelenideElement xxx = $x("//img[contains(#title,'Uwagi')]");
String str = xxx.getAttribute("title");
writer.write(str);
...
Result: picture
Regards
Tomes
Related
I am unable to get the comma separated values from the excel cell using java code.
I had tried using the following code also.
String [] items = commaSeparated.split("\\s*,\\s*");
List<String> container = Arrays.asList(items);
I want the output as a list like:
IND
PAK
USA
AUS
When the input is imported from the Excel cell as IND,PAK,USA,AUS.
If all you want to do is print each item of your CSV data on a new line, this code will do the job.
String csvLine = "IND,PAK,USA,AUS";
Arrays.stream(csvLine.split(",")).forEach(
item -> System.out.println(item)
);
I am using Jsoup to formatting an HTML string to plain text. I still want to preserve the line breaks and ignore the HTML tags. But when converting I get extra empty lines and its trowing off my string.
String htmlString = "<p>Hello this is a description. </p><p>I know Just checking how it looks.</p><p></p><p><code>Add a line.</code></p><p>This is a notmal line <span style="color:#F9931A">Adding orange</span></p><ul><li><p>one </p></li><li><p>two</p></li></ul>";
HtmlToPlainText convert = new HtmlToPlainText();
Document html = Jsoup.parse(htmlString,"", Parser.xmlParser());
String new = convert.getPlainText(html);
System.out.println("This is the description: " + new);
OUTPUT:
Hello this is a description.
I know Just chekcing how it looks.
Add a line.
This is a notmal line Adding orange
* one
* two
I'm parsing html of a website with JSoup. I want to parse this part:
<td class="lastpost">
This is a text 1<br>
Website Page - 1
</td>
I want like this:
String text = "This is a text 1";
String textNo = "Website Page - 1";
String link = "post/13594";
How can I get the parts like this?
Your code would only get all the text that is in the td elements that you are selecting. If you want to store the text in separate variables, you should grab the parts separately like the following code. Extra comments added so you can understand how/why it is getting each piece.
// Get the first td element that has class="lastpost"
Element lastPost = document.select("td.lastpost").first();
// Get the first a element that is a child of the td
Element linkElement = lastPost.getElementsByTag("a").first();
// This text is the first child node of td, get that node and call toString
String text = lastPost.childNode(0).toString();
// This is the text within the a (link) element
String textNo = linkElement.text();
// This text is the href attribute value of the a (link) element
String link = linkElement.attr("href");
I have a Java class to automate some behaviour on the web, my only problem is that now instead of the static data that I have I need to use the data from the csv.
for example:
this is one of the actions in my automation class:
WebElement supplierAddressField = driver.findElement(By.id("FieldaddressOfSupplierLine"));
supplierAddressField.sendKeys("hollywood blvd 34");
So now, instead of the static "supplier address" value I want to iterate on each line of the .sendKeys(csvLineMap.get("supplier address"));
Because in each line I dont need all the headers info, this is why I think it will be the best to just create a list of maps, that each map key will be the header of the csv and the value will be the value for this header in a specific line.
this is the structure of the csv:
Please help me to figure this out...thanksss!!
Apache Commons CSV
For what you are asking for I would recommend you look at Apache Commons CSV. One of the examples from their User Guide matches very closely with with the examples you are trying
Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
String lastName = record.get("Last Name");
String firstName = record.get("First Name");
}
ok, this might be overly complex for what you want, but I always open csv's as excel files because then you can run down the columns. The code for picking up any column would look like this:
Workbook w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet(0);
nom = sheet.getRows();
String[][] SheetArray = new String [2][nom];
// change the first number to the number of columns you want,
// or pick up the number same as you did with rows
Cell cell;
// GETS DATA FROM SHEET AND RUNS THROUGH WHOLE LOOP BELOW FOR EACH REFERENCE
for(int j =0;j<sheet.getRows();j++) // cycles through rows and loads into 2d array
{ // start 6
cell = sheet.getCell(0, j); <- your column number here
cellcont = cell.getContents();
SheetArray[0][j] = cellcont;
// repeat the above block for each column you want
} // end 6
you now have a 2d array with all the info in it which you can handle however you want.
wrap the entire thing in a try .. catch.
With uniVocity-parsers you can parse only the fields you are interested, in any order:
CsvParserSettings parserSettings = new CsvParserSettings();
// Let's extract headers
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.selectFields("Field 5", "Field 1");
//Rows will come organized according to your field selection
List<String[]> allRows = parser.parseAll("path/to/file.csv");
If you prefer, you can easily get a map with the values of all columns:
CsvParserSettings parserSettings = new CsvParserSettings();
// Let's extract headers
parserSettings.setHeaderExtractionEnabled(true);
// To get the values of all columns, use a column processor
ColumnProcessor rowProcessor = new ColumnProcessor();
parserSettings.setRowProcessor(rowProcessor);
CsvParser parser = new CsvParser(parserSettings);
//This will kick in our column processor
parser.parse(new FileReader("path/to/file.csv"));
//Finally, we can get the column values:
Map<String, List<String>> columnValues = rowProcessor.getColumnValuesAsMapOfNames();
Have a look. It is faster than any other parser and you can do much more, such as converting the values and generating java beans.
Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).
I am really unsure how I can get the information I need to place into a database, the code below just prints the whole file.
File input = new File("shipMove.txt");
Document doc = Jsoup.parse(input, null);
System.out.println(doc.toString());
My HTML is here from line 61 and I am needing to get the items under the column headings but also grab the MMSI number which is not under a column heading but in the href tag. I haven't used JSoup other than to get the HTML from the web page. I can only really see tutorials to use php and I'd rather not use it.
To get those information, the best way is to use Jsoup's selector API. Using selectors, your code will look something like this (pseudeocode!):
File input = new File("shipMove.txt");
Document doc = Jsoup.parse(input, null);
Elements matches = doc.select("<your selector here>");
for( Element element : matches )
{
// do something with found elements
}
There's a good documentation available here: Use selector-syntax to find elements. If you get stuck nevertheless, please describe your problem.
Here are some hints for that selector, you can use:
// Select the table with class 'shipinfo'
Elements tables = doc.select("table.shipinfo");
// Iterate over all tables found (since it's only one, you can use first() instead
for( Element element : tables )
{
// Select all 'td' tags of that table
Elements tdTags = element.select("td");
// Iterate over all 'td' tags found
for( Element td : tdTags )
{
// Print it's text if not empty
final String text = td.text();
if( text.isEmpty() == false )
{
System.out.println(td.text());
}
}
}