【问题标题】:Selenium list size printed in table using java使用java在表格中打印的硒列表大小
【发布时间】:2018-01-24 09:31:20
【问题描述】:

目前,我正在开展一个项目,我在其中打印一张表,我的 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>");

这是我当前的输出

【问题讨论】:

    标签: java html selenium selenium-webdriver


    【解决方案1】:

    只需替换您班级中的以下代码

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

    【讨论】:

    • 感谢 tuks 的帮助。它正在工作,但 13 数字列中的问题打印 2 次。同一列我得到 13 和 1 数字列
    • 它是否在表中插入了正确的值?或者也有与打印相同的问题?
    • 我附上了一张图片。你能检查一下吗?它显示第 1 行 13 数字列和第 2 行第 1 列相同的值也第 2 行 13 数字列和第 3 行第 1 列相同的值..但它不同
    • @zsbappa,我已经更新了答案现在检查。刚刚在else 块中添加了clink=e.getText();
    【解决方案2】:

    如何为列再添加一个循环

        for(WebElement e : rhrev) {
            String clink=e.getText();
            System.out.println(clink);
            Reporter.log("<tr>");
            for (int col=0; col< 13; col++){
                Reporter.log("<td>"+clink+ "</td>");    
            }
            Reporter.log("<tr>");
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-20
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 2016-02-15
      相关资源
      最近更新 更多