How to store WebTable data into Hash Table

I have a WebTable and i want to store all webtable values to a HashTable.I’m able to retrive all webtable value . Kindly advise how to store value into HashTable.

 

WebTable :

 

<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>

<table name=”BookTable”>
<tr>
<th>BookName</th>
<th>Author</th>
<th>Subject</th>
<th>Price</th>
</tr>
<tr>
<td>Learn Selenium</td>
<td>Amit</td>
<td>Selenium</td>
<td>300</td>
</tr>
<tr>
<td>Learn Java</td>
<td>Mukesh</td>
<td>Java</td>
<td>500</td>
</tr>
<tr>
<td>Learn JS</td>
<td>Animesh</td>
<td>Javascript</td>
<td>300</td>
</tr>
<tr>
<td>Master In Selenium</td>
<td>Mukesh</td>
<td>Selenium</td>
<td>3000</td>
</tr>
<tr>
<td>Master In Java</td>
<td>Amod</td>
<td>JAVA</td>
<td>2000</td>
</tr>
<tr>
<td>Master In JS</td>
<td>Amit</td>
<td>Javascript</td>
<td>1000</td>
</tr>
</table>

</body>
</html>

 

 

Code:

 

public class webtable {

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “D:\\Selenium\\chromedriver.exe”);

WebDriver cd=new ChromeDriver();

cd.get(“file://C:/Users/User/Desktop/test.html”);
cd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String WE=cd.findElement(By.xpath(“//table[@name=’BookTable’]/tbody/tr[3]/td[3]”)).getText();

System.out.println(WE);

List<WebElement> rows = cd.findElements(By.xpath(“//table[@name=’BookTable’]/tbody/tr”));
System.out.println(“No of rows is : ” + rows.size());

List<WebElement> cols = cd.findElements(By.xpath(“//table[@name=’BookTable’]/tbody/tr[1]/td”));
System.out.println(“No of cols is : ” + cols.size());

String first_part = “//table[@name=’BookTable’]/tbody/tr[“;
String second_part = “]/td[“;
String third_part = “]”;

for(int i=1;i<=rows.size();i++) {

for(int j=1;i<=cols.size()-1;j++) {

String final_xpath = first_part+i+second_part+j+third_part;

String Table_data = cd.findElement(By.xpath(final_xpath)).getText();
System.out.print(Table_data +” “);

}
}

 

}

}

 

Default Asked on April 5, 2019 in Selenium WebDriver.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.