zsbappa zsbappa - 1 year ago 75
HTML Question

Selenium list size printed in table using java

Currently, I am working one project where I print table one my testNG emailable report
the problem is I am not getting the way that how to divide it on another row? Like every 13 row, a new row will start and I will give the one-row name.Thanks for supporting me

@Test
public void Oz_Stays_dashboard() throws InterruptedException {
wd.manage().window().maximize();

Thread.sleep(5000);
wd.findElement(By.xpath(".//*[@id='login-form']/div[4]/input")).click();
Thread.sleep(5000);
wd.findElement(By.xpath(".//*[@id='mws-navigation']/ul/li[1]/a/table/tbody/tr/td[2]")).click();
wd.findElement(By.xpath(".//*[@id='mws-navigation']/ul/li[1]/ul/li[2]/a")).click();
Thread.sleep(5000);
Reporter.log("<!DOCTYPE html>");
Reporter.log("<html>");
Reporter.log("<head>");
Reporter.log("<style>");
Reporter.log("table, th, td {");
Reporter.log(" border: 1px solid black;");
Reporter.log("border-collapse: collapse;");
Reporter.log("}");
Reporter.log("th, td {");
Reporter.log("padding: 5px;");
Reporter.log("text-align: left;");
Reporter.log("}");
Reporter.log("</style>");
Reporter.log(" </head>");
Reporter.log("<body>");
Reporter.log("<table>");
Reporter.log(" <tr>");
Reporter.log("<th>Ad Rev.</th>");
Reporter.log("<th>Lead Rev.</th>");
Reporter.log("<th>Booking Rev.</th>");
Reporter.log("<th>Homeaway Rev.</th>");
Reporter.log("<th>VRBO Rev.</th>");
Reporter.log("<th>Hotels.com Rev.</th>");
Reporter.log("<th>Airbnb Rev.</th>");
Reporter.log("<th>Total Projected Rev.</th>");
Reporter.log("<th>Ad Spend</th>");
Reporter.log("<th>G.Profit</th>");
Reporter.log("<th>G.Margin</th>");
Reporter.log("<th>Ad Rev/Spend</th>");
Reporter.log("<th>ROAS</th>");
Reporter.log(" <tr>");
List<WebElement> rhrev = wd.findElements(By.xpath(".//*[@class='mws-stat-value']"));

int size=rhrev.size();
**for(WebElement e : rhrev) {
String clink=e.getText();
System.out.println(clink);

Reporter.log("<th>"+clink+ "</th>");

}**
Reporter.log(" </tr>");
Reporter.log("</table>");
Reporter.log("</body>");
Reporter.log("</html>");


and here is my current Output
enter image description here

enter image description here

Answer Source

Just replace below code in your class

int count =1;
String clink=null;
List<WebElement> rhrev = wd.findElements(By.xpath(".//*[@class='mws-stat-value']"));           
int size=rhrev.size();

for(WebElement e : rhrev) 
{
    clink=e.getText();
    System.out.println(clink);

    if(count<=13)
    {
        Reporter.log("<td>"+clink+ "</td>");
        count++;
    }
    else
    {

        count=1;
        clink=e.getText();
        Reporter.log(" </tr>");
        Reporter.log(" <tr>"); 
        Reporter.log("<td>"+clink+ "</td>");
        count++;
    }
}

You need to add add new ROW after every 13th column. This code will help. I have tested at my end, its working fine. If face any issue let me know :)

Complete Code would be :

Reporter.log("<!DOCTYPE html>");
Reporter.log("<html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; }");
Reporter.log("th, td { padding: 5px; text-align: left; }");
Reporter.log("</style> </head>");
Reporter.log("<body> <table> <tr>");
Reporter.log("<th>Ad Rev.</th> <th>Lead Rev.</th> <th>Booking Rev.</th> <th>Homeaway Rev.</th> <th>VRBO Rev.</th>");
Reporter.log("<th>Hotels.com Rev.</th> <th>Airbnb Rev.</th> <th>Total Projected Rev.</th> <th>Ad Spend</th> <th>G.Profit</th> <th>G.Margin</th> <th>Ad Rev/Spend</th> <th>ROAS</th>");
Reporter.log(" <tr>");      
List<WebElement> rhrev = wd.findElements(By.xpath(".//*[@class='mws-stat-value']"));           
int count =1;
String clink=null;
for(WebElement e : rhrev)
{
    if(count<=13)
    {
        clink=e.getText();
        System.out.println(clink);
        Reporter.log("<td>"+clink+ "</td>");
        count++;
    }
    else
    {
        count=1;
        clink=e.getText();
        Reporter.log(" </tr> <tr>");
        Reporter.log("<td>"+clink+ "</td>");
        count++;                      
    }                 
}
Reporter.log(" </tr> </table> </body> </html>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download