Sunday, December 21, 2014

Save Webpage dropdown value in notepad using Selenium-Webdriver


In this Example:
  1. Am navigating to ebay.in
  2. Moving moving to "My ebay" using 
Actions act = new Actions(driver);
act.moveToElement(service).perform();

    3.  loop all the values in the list using xpath("By.xpath("//*[@id='gh-eb-My-o']/ul/li"")
    4.  Storing in a notepad.





Source Code :

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class test_drop extends Thread {
    public static WebDriver driver;
    public static void main(String[] args) throws IOException {
        driver = new FirefoxDriver();
        driver.get("http://www.ebay.in/");
        WebElement service = driver.findElement(By.xpath("//a[text()='My eBay']"));
        Actions act = new Actions(driver);
        act.moveToElement(service).perform();
        List<WebElement> dropdown = driver.findElements(By.xpath("//*[@id='gh-eb-My-o']/ul/li"));
        System.out.println(dropdown.size());
        
        FileWriter fileWriter = new FileWriter("D:\\output.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        
        
        
        for(WebElement ele: dropdown){
            System.out.println(ele.getText());
            bufferedWriter.write(ele.getText()+"\n");
            
        }
        bufferedWriter.close();
        
        driver.close();
    }
}

Hope you like this post.
Hit Like button on facebook.


No comments:

Post a Comment