java - 使用java在表格中打印Selenium列表大小

标签 java html selenium selenium-webdriver

目前,我正在开展一个项目,其中打印表一我的 testNG 可通过电子邮件发送的报告 问题是我不知道如何将它划分到另一行?与每 13 行一样,将开始一个新行,我将给出单行名称。感谢您支持我

@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>");

这是我当前的输出 enter image description here

enter image description here

最佳答案

只需替换您的类中的以下代码

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++;
    }
}

您需要在每第 13 列之后添加新的 ROW。这段代码会有所帮助。我最后测试过,工作正常。如果遇到任何问题,请告诉我:)

完整的代码是:

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>");

关于java - 使用java在表格中打印Selenium列表大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45710171/

相关文章:

html - 抑制链接标题

html - 居中行内 block 元素后的空白

python - 无法使用 selenium 取消选择已选择的空白区域

excel - 自动化错误。通过 Selenium 和 VBA 使用 Excel 的灾难性故障错误

python - 使用 Selenium Python 滚动到底部

java - Spring Boot 分段上传获取空文件对象

java - JSP 生成 .xml 文件

java - ObjectInputStream/ObjectOutputStream 工作不正常

java - OpenCV使用异常

jquery - 导航左侧不必要的空间