如何使用java和selenium webdriver从特定日期范围的datagrid读取数据

时间:2015-01-07 10:07:21

标签: java excel datagrid selenium-webdriver apache-poi

场景:例如想要获取列名:所有者名称|共享举行|日期来自网站的数据网格:nasdaq.com/symbol/ctsh/institutional-holdings 日期范围为2014年1月1日至2014年12月31日或2013年1月1日2013年12月31日,并希望把它放入excel表。请指导我。

==============

代码我设计此代码以从数据网格中读取数据,现在所有页面都在寻找上述方案的解决方案。

 public class NasdaqReadTabaleData {

    // extends Nasdaqe_InvesterListData{
public static WebDriver driver = new FirefoxDriver();

@Test
public void NasdaqeTable() throws Exception {

    driver.get("http://www.nasdaq.com/symbol"); 
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   /* for (int i = 1; i < s.getRows(); i++)
    {
    //Read data from excel sheet
    String s1 = s.getCell(0,i).getContents();*/
    driver.findElement(By.id("stock-search-text")).clear();
    driver.findElement(By.id("stock-search-text")).sendKeys("CTSH");
    driver.findElement(By.id("stock-search-submit")).click();

    JavascriptExecutor jsedown = (JavascriptExecutor)driver;
    jsedown.executeScript("scroll(0, 250)");

    //Click on holdings link.
    driver.findElement(By.id("holdingslink")).click();
    System.out.println("Action should counduct once  ");
    Thread.sleep(3000);


    try
    {
        JavascriptExecutor jsedownsmall = (JavascriptExecutor)driver;
        jsedownsmall.executeScript("scroll(0, 950)");

        WebElement Webtable =driver.findElement(By.id("secfilingContainer")); 
        if(Webtable.isDisplayed()){

            // Replace TableID with Actual Table ID or Xpath
        List<WebElement> TotalRowCount=Webtable.findElements(By.tagName("tr"));
        System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size());

        // Now we will Iterate the Table and print the Values   
            int RowIndex=1;
            for(WebElement rowElement:TotalRowCount)
            {
                  List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td[3]"));
                  int ColumnIndex=1;
                  for(WebElement colElement:TotalColumnCount)
                  {
                       System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+colElement.getText());
                       ColumnIndex=ColumnIndex+1;

                   }
                  RowIndex=RowIndex+1;
            } 

            // Paging functionality For All Paages 

        // locator for last page button         
        List<WebElement> lastPage = driver.findElements(By.xpath("html/body/div[4]/div[2]/div[9]/form/div[15]/div/ul/ul/li"));
        System.out.println("Paging functionality");

        for(int i=1; i<=(lastPage.size()); i++)
            {
            if (lastPage.size()>=0){
                //locator for next page button
                List<WebElement> nextPage = driver.findElements(By.id("quotes_content_left_lb_NextPage"));
                if(nextPage.size() >= 1){
                    Thread.sleep(2000);
                    nextPage.get(0).click();

                    JavascriptExecutor jsedownsmall1 = (JavascriptExecutor)driver;
                    jsedownsmall1.executeScript("scroll(0, 950)");

                    WebElement Webtable1 =driver.findElement(By.id("secfilingContainer")); 

                    if(Webtable1.isDisplayed()){
                        List<WebElement> TotalRowCount1=Webtable1.findElements(By.tagName("tr"));

                            System.out.println("No. of Rows in the WebTable: "+TotalRowCount1.size());

                            // Now we will Iterate the Table and print the Values   
                            int RowIndex1=1;
                            for(WebElement rowElement:TotalRowCount1)
                            {
                                List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td[3]"));
                                int ColumnIndex=1;
                                for(WebElement colElement:TotalColumnCount)
                                {
                                    //System.out.println("Current Page ::"+lastPage);
                                    System.out.println("Row "+RowIndex1+" Column "+ColumnIndex+" Data "+colElement.getText());
                                    ColumnIndex=ColumnIndex+1;

                                    WebElement excelData[][] = new WebElement[1][10];
                                    excelData[0][0] = colElement;

                                }
                                RowIndex1=RowIndex1+1;
                            } 
                    }
                }else lastPage.get(0).click();
            }
            }
        }
        }
    catch(Exception e){ 
        e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我正在尝试下面提到的代码来读取日期范围内的数据,但我认为这是不合适的。

@Test      public void print_data()throws Exception {          String startDate =“01/01/2014”;          String endDate =“09/30/2014”;

String final_xpath = first_part+k+forth_part;
String Table_data = driver.findElement(By.xpath(final_xpath)).getText(); (Hear am reading three column Owner Name | Shared Held | Date )

if(Table_data.contains(startDate)|| Table_data.contains(endDate)){(想要在2014年1月1日至2014年12月31日的日期范围内编写逻辑选择数据)

System.out.print(Table_data1 +"  ");// to print on console
excelLog(i,Table_data2,Table_data3,Table_data4); // To print in excel sheet