0

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

Sers
  • 12,047
  • 2
  • 12
  • 31
tomes
  • 1
  • 1

2 Answers2

0

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+";");
Sers
  • 12,047
  • 2
  • 12
  • 31
  • Hi, @Sers thanks for quick response. Unfortunately this solution doesn't work.
    String uwagiTitle = uwagi.text().replace("\n", " "); returns nothing. I've also tried:
     SelenideElement xxx = $x("//img[contains(@title,'Uwagi')]");
            String str = xxx.getAttribute("title").replace("\n","aa");
    
            System.out.println(str);  
    but it also returns only: "Data urodzenia: 25.02.1808 r." instead of "Uwagi: łacina, nieczytelne Data urodzenia: 25.02.1808 r.". Sorry for comment formatting I'm new on stackoverflow.
    – tomes Feb 25 '19 at 10:15
0

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

tomes
  • 1
  • 1