简体   繁体   中英

Retrieve data from a field using selenium

I am a begineer in Automation Testing(using Selenium, JavaLang N Eclipse).

I am trying to fetch few values from a webpage and store them in an excel.

The input values are taken from excel and the output will be stored in an excel.

The problem currently i am facing is, let me tell you with an example.

Firstly, it takes account number say 123 from excel and inputs in to search field and then selects the appropriate account and fetches name and email.

Secondly the next account number say 234 is fetched from excel and inputs in to search field and then it gets the appropriate name and email of that account(234).

I am trying with just two accounts as if now.

So when i see the ouput or rather opens the excel file i only get the last account owner name and number in both the columns.

The result i needed is First account name and email in one column and second account name and email in another column.

I request all the folks to help me out with this!!.

Thanks for looking in to this..

int rc= ws.getRows();

Below is the code:

WebElement s1=driver.findElement(By.xpath("//table[@id='kInfo']/tbody/tr[10]/td"));
       //WebElement s1=driver.findElement(By.xpath("//td[@class='tRight']/a"));
          String s2=s1.getText();
          System.out.println("data:"+s2);

          String D1= driver.findElement(By.xpath("//div[@class='vcard clearfix']")).getText();
          System.out.println(D1);
       Thread.sleep(1000);
       /*driver.findElement(By.xpath("//*[@id='homePage']")).click();
       driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
       Thread.sleep(1000);*/

       WritableWorkbook wb1 = Workbook.createWorkbook( new File("C:\\Bhasker-DNT\\Excel\\ouput.xls"));
       WritableSheet ws1 = wb1.createSheet("customsheet", 1);
       {
        for(int count=0;count<4;count++)
        {
            for(int i=0;i<rc-1;i++)
            {
            Label label = new Label(i, 0, s2);
               ws1.addCell(label);

               Label label1 = new Label(i, 1, D1);
               ws1.addCell(label1);
            }           
        }
       //Label label = new Label(0, 0, s2); 
       //ws1.addCell(label);
       }
       wb1.write();
       wb1.close();

To fetch element for a div or a selection or ap you need the xpath i suppose you have it. Than

String name = ""
    List<WebElement> your_list= driver.findElements(By.xpath("xpath_stuff or workbook.get(I)"));


                for (int i=0; i<your_list.size(); i++) {
                //  System.out.println(i+ ". " + your_list.get(i).getText());
                    // here you can write the elements to excel.
  //if you need first name you need to just
                   if(i==(you know which is the first row that conatins the name)
                        {
                          name = your_list.get(i).getText();
                         }


   ... the same way the email and the other
                ws1.addCell(your_list.get(i).getText());
                }

       wb1.close();

I hope it helps, if not please provide us some example from the html and the from excel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM