Selenium: Not able to print all the values from a list stored of IMDB Top Movies and Ratings

Viewed 912

I am having trouble printing all the values from a list.

Below is the complete code:

public class TestCase1{

private static XSSFWorkbook wb;
private static XSSFSheet sh;
private static File file;
static int i=0;

public static void main(String args[]) throws Exception
{
    file = new File("D:\\Eclipse and workspace\\workspace\\SeleniumPractice\\IMDBTestCase1.xlsx");
    FileInputStream fIs = new FileInputStream(file);
    wb = new XSSFWorkbook(fIs);
    sh = wb.getSheet("Sheet1");
    sh.createRow(i);

    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse and workspace\\eclipse\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();

    driver.get("http://www.imdb.com");

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(By.id("navTitleMenu"))).build().perform();

    Thread.sleep(5000);

    driver.findElement(By.xpath("//div[@id='navMenu1']/div[2]/ul[1]/li[6]/a")).click();

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='chart full-width']/thead/tr/th[2]")));

    WebElement elem1 = driver.findElement(By.xpath("//table[@class='chart full-width']/thead/tr/th[2]"));
    String title = elem1.getText();
    write(0,0,title);

    WebElement elem2 = driver.findElement(By.xpath("//table[@class='chart full-width']/thead/tr/th[3]"));
    String rating = elem2.getText();
    write(0,1,rating);
    i++;

    List <WebElement> elements = driver.findElements(By.className("lister-list"));
    for(WebElement element : elements)
    {   
        String str1 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[2]")).getText();
        System.out.println(str1);
        //write(i,0,elem3.getText());

        String str2 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[3]")).getText();
        System.out.println(str2);
        //write(i,1,elem4.getText());

        i++;
    }
    System.out.println("Success");

    wb.close(); 
}

public static void write(int row, int col, String val) throws Exception
{
    FileOutputStream fos = new FileOutputStream(file);
    sh.createRow(row+1);
    sh.getRow(row).createCell(col).setCellValue(val);
    wb.write(fos);
}

}

Now in the below part,

List <WebElement> elements = driver.findElements(By.className("lister-list"));
    for(WebElement element : elements)
    {   
        String str1 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[2]")).getText();
        System.out.println(str1);
        //write(i,0,elem3.getText());

        String str2 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[3]")).getText();
        System.out.println(str2);
        //write(i,1,elem4.getText());

        i++;
    }

Here the output is printing only this:

1. The Shawshank Redemption (1994)
9.2
Success

I want to print all the movies names and their ratings but I am not sure why it is printing only the first one. Also if I use the below for loop, it will print all the names and ratings but I want them separately as I want to store them in an excel file.

for(WebElement element : elements)
    {   
        System.out.println(element.getText());
    }

PS: The real thing here I am trying to do is to store the movies names and their ratings in an excel file.

1 Answers
Related