简体   繁体   中英

Selenium read a table and write into a file line by line

I am looking for the help to read a table span using selenium code and write span into the file, following is my html code

<table>
<tbody><tr>
<tr>
<td><span>
FIRST
</span>
</td>
</tr>
<tr>
<td>
<span>SECOND</span>
</td>
</tr>
<tr>
<td>
<span>THIRD</span>
</td>
</tr>
<tbody>
</table> 

I need to write FIRST SECOND THIRD on a file in java.

Thanks a lot.

I suppose you have located/found the table WebElement. Then you can get span elements content like this:

List<WebElement> spanElements = tableElement.findElements(By.ByTagName("span"));

for (WebElement element : spanElements) {
    String spanContent = element.getText();
    //save it to a collection or a StringBuilder, then write it to a file
}

Having a look at this and this might help.

1) XPath that gets all text is:

//span/text()

2) in java code you may type something like

String text = selenium.getText("xpath=//span"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM