How to read data from datagrid in specific date range using java and selenium webdriver

1,725 views
Skip to first unread message

Sagar Chaudhari

unread,
Jan 8, 2015, 5:22:58 AM1/8/15
to webd...@googlegroups.com

Scenario : Want to fetch Column name: Owner Name | Shared Held | Date from data grid of
website: nasdaq.com/symbol/ctsh/institutional-holdings
In date range say 01/01/2014 to 31/12/2014 or 01/01/2013to 31/12/2013 and want put it into excel sheet. please guide me on this.

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

Code I designed this code to read data from data grid and all pages now am looking for solution for above mention Scenario.


public class table1 {

   
     WebDriver driver = new FirefoxDriver();
     protected static HSSFWorkbook wb = new HSSFWorkbook();  // or new XSSFWorkbook();
     protected static HSSFSheet sheet1 = wb.createSheet("IHSharedHeld");
     
     
     @BeforeTest
        public void setup() throws Exception {
             driver.manage().window().maximize();
             driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
             driver.get("http://www.nasdaq.com/symbol");
              driver.findElement(By.id("stock-search-text")).clear();
             driver.findElement(By.id("stock-search-text")).sendKeys("BDC");
             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);
        }
     
     @AfterTest
     public void tearDown() throws Exception {
       driver.quit();
         }
     @Test
     public void print_data() throws Exception{

//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=0; 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() >= 0){
                                   
                                    //Get number of rows In table.
                                     int Row_count = driver.findElements(By.xpath(".//*[@id='quotes_content_left_pnlInsider']/table/tbody/tr")).size();
                                     System.out.println("Number Of Rows = "+Row_count);
                                        
                                    //Get number of columns In table.
                                    int Col_count = driver.findElements(By.xpath(".//*[@id='quotes_content_left_pnlInsider']/table/tbody/tr[1]/td")).size();
                                    System.out.println("Number Of Columns = "+Col_count);
                                               
                                    //divided xpath In three parts to pass Row_count and Col_count values.
                                    String first_part = ".//*[@id='quotes_content_left_pnlInsider']/table/tbody/tr[";
                                    String second_part = "]/td[";
                                    String third_part = "]";
                                               
                                    //Used for loop for number of rows.
                                    for (int k=1; k<=Row_count; k++){
                                        //Used for loop for number of columns.
                                        for(int j=1; j<=3; j++){
                                            //Prepared final xpath of specific cell as per values of i and j.
                                            String final_xpath = first_part+k+second_part+j+third_part;
                                           
                                            //Will retrieve value from located cell and print It.
                                            String Table_data = driver.findElement(By.xpath(final_xpath)).getText();
                                            System.out.print(Table_data +"  ");
                                           
                                            excelLog(Table_data);
                                           
                                            }
                                        System.out.println("");
                                        System.out.println("");
                                       
                                    }
                                }
                                nextPage.get(0).click();
                               
                            }
                           
                       
                       
                        }
       
     }

Now I want only write Owner Name | Shared Held | Date column data in to excel sheet in date range of
say 01/01/2014 to 31/12/2014 or 01/01/2013to 31/12/2013.

Please Help me on this topic

darrell

unread,
Jan 9, 2015, 9:00:56 PM1/9/15
to webd...@googlegroups.com
What is your WebDriver question? So far it looks like you are asking us to do your work for you.

Sagar Chaudhari

unread,
Jan 12, 2015, 2:13:02 AM1/12/15
to webd...@googlegroups.com
Hello Darrell,

my question is How to read data in 2 dates from data grid ,

this is my HTML Code

<div id="quotes_content_left_pnlInsider">
<table>
<thead>
<tbody>
<tr>
<td> (This is Colum1 TickerName)
<td align="center">09/30/2014</td> (This is Colum2 Date )
<td align="center">5,175,609</td>(This is Colum2 Amount )
<td align="center">460,264</td>
<td align="center">9.76</td>
<td align="center">132,547</td>
</tr>
 so i want to print data which is in spacific date range , please help me how to implement in my current code as mention above.

Regards
SagarC

darrell

unread,
Jan 12, 2015, 9:20:59 AM1/12/15
to webd...@googlegroups.com
If you want to get all the dates for a page you would use:

    int rows = driver.findElements(By.cssSelector("#quotes_content_left_pnlInsider tbody>tr")).size();
    for(int row = 1; row < rows; row++) {
        WebElement dateElement = driver.findElement(By.cssSelector("#quotes_content_left_pnlInsider tbody>tr:nth-of-type(" + row + ")>td:nth-of-type(2)"));
        String date = dateElement.getText();
        // convert date string into a Date object
        // use the Date object methods to determine if it is within the desired range
        // if the date is within the desired range save the row
    }

Normally I would just get all the elements in one shoot using driver.findElements() to return a List<WebElement> but the page you are examining has javascript which modifies the DOM. So it is safer to get elements one at a time.

Sagar Chaudhari

unread,
Jan 14, 2015, 2:46:16 AM1/14/15
to webd...@googlegroups.com
Hi ,

am trying below mention code to read data in date rang , but i think it is not proper .


 @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)){ (Want to write logic pick data in date range 01/01/2014 to 31/12/2014)

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

darrell

unread,
Jan 14, 2015, 1:26:22 PM1/14/15
to webd...@googlegroups.com
This is getting more into dealing with dates in Java then it is WebDriver. The WebDriver code should get the Owner, the Shared Held and the Date as three separate WebDriver calls. For example,

            String startDate = "01/01/2014";    // MM/DD/YY
            String endDate = "12/31/2014";      // MM/DD/YY
            int rows = driver.findElements(By.cssSelector("#quotes_content_left_pnlInsider tbody>tr")).size();
            for(int row = 0; row < rows; row++) {
                Date date = getDate(row+1);
                if(checkDateRange(startDate, endDate, date)) {
                    String ownerName = getOwnerName(row+1);
                    String sharedHeld = getSharedHeld(row + 1);
                    Share share = new Share(ownerName, date, sharedHeld);
                    shares.add(share);
                }
            }

This code would locate how many rows on the current page then the for-loop gets the date for each row. It does not get the entire row and parse the string into the parts although it could. Once I get the date into the variable 'date' I can then use Java Date object to compare the dates. So if getDate was:

    private Date getDate(int row) {
        DateFormat df = new SimpleDateFormat("MM/dd/yy");
        By by = By.cssSelector("#quotes_content_left_pnlInsider tbody>tr:nth-of-type(" + row + ") td:nth-of-type(2)");
        wdw.until(ExpectedConditions.visibilityOfElementLocated(by));
        try {
            return df.parse(driver.findElement(by).getText());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

It will get the date field for the current row. It then converts the string to a Date object and returns it. The checkDateRange method then converts the startDate and endDate strings into Date objects. Once I have three Date objects I can return something like:

        return (date.compareTo(start) >= 0 && date.compareTo(end) <= 0);

This will make sure 'date' is between 'start' and 'end'. If the current date is in the desired range I return true, else return false.So now my main loop will only get the other two columns and save them to a List if the date is in range. When I exit the for-loop the List 'shares' will have all the rows which are in the desired date range. I can then wrap the for-loop in a do-while loop for each page of data. The do-while loop would exit when there are no more pages left.

After that you have a List<Share> of shares. This is a pure Java object. Use an Excel compatible library like Apache POI to iterator over the List and write it to an Excel spreadsheet.
Reply all
Reply to author
Forward
0 new messages