如何使用Selenium识别Web表中的最大行数

时间:2014-03-23 11:48:12

标签: java selenium selenium-webdriver

大家好,我写了一些代码来访问从网站到Excel的表值。但问题是我找不到计算表中可用行数的解决方案。所以如果有任何解决方案请建议我所以我可以解决我的问题。     我的代码是:

import static org.junit.Assert.*;

    import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

    import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

    import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;


    public class ExportinExcel {
        public static WebDriver driver;
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
           driver=new FirefoxDriver();
          driver.navigate().to("http://www.indianrail.gov.in/tatkal_Scheme.html");
           driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);

        }

        @AfterClass
        public static void tearDownAfterClass() throws Exception {
        driver.quit();
        }

        @Test
        public void test() throws IOException, RowsExceededException, WriteException {
            //fail("Not yet implemented");
            // Given the path where to store Excel.
File FExcel = new File("D:\\software\\Excel\\createExcel"+hashCode()+".xls");
                 /* Create a Workbook. */
            WritableWorkbook workbookexcel= Workbook.createWorkbook(FExcel);
                 /* Create a Worksheet. */
            workbookexcel.createSheet("Data", 0);
            WritableSheet writeablesheet= workbookexcel.getSheet(0);
                /* Add Content in row and column and here coumn value increment each time.   */  
//          jxl.write.Label Data1 = new jxl.write.Label(row,column,                 driver.findElement(By.xpath(".//tr[1]/td[1]/p/b/span")).getText());
            int i=0;int j;int x=1; int y; 
           while(i<3)
           { j=0;
               for(;j<3;)
               { 
       **// Here i have to entered Manually Number of data as 7 which i need dynamically.**                  
                   for(y=1;y<=7;y++)
                   {
                        jxl.write.Label Data1 = nw jxl.write.Label(i, j, driver.findElement(By.xpath(".//tr["+y+"]/td["+x+"]/p/b/span")).getText());
                        writeablesheet.addCell(Data1);
                        j++; 
                   }x++;
                }i++;
               }
System.out.print("11");
workbookexcel.write();
            workbookexcel.close();
        }
}

1 个答案:

答案 0 :(得分:0)

你去了:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

...

WebDriver driver = new FirefoxDriver();
driver.get("http://www.indianrail.gov.in/tatkal_Scheme.html");
WebElement table = driver.findElement(By.className("MsoNormalTable"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
int numOfRows = rows.size();