我不明白为什么会出现这个错误。有没有解决办法?

时间:2017-10-10 11:03:04

标签: java selenium

这是我的代码,for循环在(i == 7)失败,但i = 1到6次传递相同的元素。我不知道为什么。 我正在尝试自动化网站下载电子PDF文件。 谁能帮我吗?

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"));
    }
}

我已经提到过我的代码中循环失败的地方。 我也尝试了不同的方法来使它变好,我感到难过,我无法解决它。

  

错误:启动ChromeDriver 2.32.498550   (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a)在14875港口只有当地的   允许连接。 log4j:WARN找不到appender   logger(org.apache.http.client.protocol.RequestAddCookies)。 log4j的:WARN   请正确初始化log4j系统。 log4j:警告请参阅   http://logging.apache.org/log4j/1.2/faq.html#noconfig了解更多信息。   线程“main”中的异常org.openqa.selenium.WebDriverException:   未知错误:元素...无法点击   点(675,505)。其他元素将收到点击:   ...(会话信息:chrome = 61.0.3163.100)(驱动程序信息:   chromedriver = 2.32.498550   (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform = Windows NT   6.1.7600 x86_64)(警告:服务器未提供任何堆栈跟踪信息)   命令持续时间或超时:47毫秒

link to error readout

1 个答案:

答案 0 :(得分:2)

在这些代码行中:

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

downlinks.get(i)获取索引i的元素。索引以0开头,因此最后一个元素位于索引size() - 1。以下是发生的事情:

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

快速修复只是替换

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

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