-3

This is my code, the for loop fails at (i == 7), but i = 1 to 6 passes with same elements. I don't know why. I'm trying to automate the website to download the e-PDF files. Can anyone help me out?

public class bookboon {
static WebDriver d  ;
public static void main(String[] args) throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe");

    String downloadFilepath = "C:\\Users\\User_2\\Desktop\\bookboon\\";
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", chromePrefs);
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);


    d = new ChromeDriver(cap);
    d.get("http://bookboon.com/en/accounting-ebooks");
    int rowCount = 1;
    Row row;
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("bookboon");



    Object[][] Title= {
            {"Title","author","Pages","Discription",},
                };
        for (Object[] aBook : Title) {
            row = sheet.createRow(0);
            int columnCount = 0;
                for (Object field : aBook) {
                    Cell cell = row.createCell(columnCount++);
                        if (field instanceof String) {
                            cell.setCellValue((String) field);
                        } else if (field instanceof Integer) {
                                  cell.setCellValue((Integer) field);
                            }
                    }       
        }


        try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\User_2\\Desktop\\bookboon\\bookboon_files\\bookboon.xlsx")){ 
            workbook.write(outputStream);
            } 

        List<WebElement> downlinks = d.findElements(By.className("pdf"));
        for(int i=1;i<=downlinks.size();i++) { // problem is when it comes to i==7.
            downlinks.get(i).click();
                if(i>=2) {
                  d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[3]/a")).click();//loop fails here 
                  d.findElement(By.id("email")).sendKeys("asd@ymail.com");
                  WebElement One=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[1]/input"));
                  One.sendKeys("Studying");
                  One.sendKeys(Keys.TAB);
                  WebElement Two=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[2]/input"));
                  Two.sendKeys("Engineer/Science MSc");
                  Two.sendKeys(Keys.TAB);
                  WebElement Three=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[3]/input"));
                  Three.sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
                  Three.sendKeys(Keys.TAB); 
                  d.findElement(By.xpath(".//*[@id='download']/form/button")).click();
            }



        else {  
            d.findElement(By.id("email")).sendKeys("asd@ymail.com");
            WebElement One=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[1]/input"));
            One.sendKeys("Studying");
            One.sendKeys(Keys.TAB);
            WebElement Two=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[2]/input"));
            Two.sendKeys("Engineer/Science MSc");
            Two.sendKeys(Keys.TAB);
            WebElement Three=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[3]/input"));
            Three.sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
            Three.sendKeys(Keys.TAB); 
            d.findElement(By.xpath(".//*[@id='download']/form/button")).click();
        }
        Thread.sleep(1000); 
        d.navigate().back();
        try {
            WebElement l1 =d.findElement(By.xpath("html/body/div[1]/div/article/div[2]/article["+i+"]/a/h3")) ;
            WebElement l2 =d.findElement(By.xpath("html/body/div[1]/div/article/div[2]/article["+i+"]/a/div[1]")) ;
            WebElement l3 =d.findElement(By.xpath("html/body/div[1]/div/article/div[2]/article["+i+"]/a/div[2]/span[4]")) ;
            WebElement l4 =d.findElement(By.xpath("html/body/div[1]/div/article/div[2]/article["+i+"]/a/p")) ;

            Object[][] bookData = {
             {l1.getText(),l2.getText(),l3.getText(),l4.getText()},
                                 };  
 for (Object[] aBook : bookData) {
              row = sheet.createRow(rowCount);
 //  System.out.println("before "+rowCount);
              rowCount = rowCount+1 ;
 //  System.out.println("after "+rowCount);
              int columnCount = 0;
                        for (Object field : aBook) {
                            Cell cell = row.createCell(columnCount++);
                            if (field instanceof String) {
                                cell.setCellValue((String) field);
                            } else if (field instanceof Integer) {
                        cell.setCellValue((Integer) field);
                                }
                            }           
    }
            } catch (Exception e) {
            e.printStackTrace();
                    }


        try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\User_2\\Desktop\\bookboon\\bookboon_files\\bookboon.xlsx")) 
        {
            workbook.write(outputStream);
        }
        downlinks = d.findElements(By.className("pdf"));
    }
}

I have mentioned where the loop fails in my code . I also tried different methods to make it good , i feel sad that i couldn't fix it .

Error: Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 14875 Only local connections are allowed. log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (675, 505). Other element would receive the click: ... (Session info: chrome=61.0.3163.100) (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7600 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 47 milliseconds

link to error readout

James Jones
  • 3,850
  • 5
  • 25
  • 44
  • 1
    Please read [ask], especially the part about [mcve] (MCVE), and [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) This will help you debug your own programs and solve problems for yourself. If you do this and are still stuck you can come back and post your MCVE, what you tried, and the execution result including any error messages so we can better help you. Also provide a link to the page and/or the relevant HTML. – JeffC Oct 10 '17 at 13:15
  • Possible duplicate of [Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el) – undetected Selenium Oct 10 '17 at 15:35

1 Answers1

2

In these lines of code :

List<WebElement> downlinks = d.findElements(By.className("pdf"));
for(int i=1;i<=downlinks.size();i++) {
    downlinks.get(i).click();

downlinks.get(i) gets you the element at index i. The index starts with 0, so the last element is at index size() - 1. Here is what happens :

downlinks.get(0) // first element
downlinks.get(1) // second element, you started here
// . . .
downlinks.get(downlinks.size() - 1) // last element
downlinks.get(downlinks.size()) // out of bounds, you stopped here

The quick fix for that is simply replacing

for(int i=1;i<=downlinks.size();i++) {

with

for(int i = 0;i < downlinks.size();i++) {
Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27