【问题标题】:I don't understand why this error shows up. Is there any solution?我不明白为什么会出现这个错误。有什么解决办法吗?
【发布时间】:2017-10-10 11:03:03
【问题描述】:

这是我的代码,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 找不到附加程序 记录器(org.apache.http.client.protocol.RequestAddCookies)。 log4j:警告 请正确初始化log4j系统。 log4j:WARN 见 http://logging.apache.org/log4j/1.2/faq.html#noconfig 了解更多信息。 线程“主”org.openqa.selenium.WebDriverException 中的异常: 未知错误:元素...不可点击 点 (675, 505)。其他元素会收到点击: ...(会话信息:chrome=61.0.3163.100)(驱动程序信息: 铬驱动程序=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),平台=Windows NT 6.1.7600 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:47 毫秒

link to error readout

【问题讨论】:

标签: java selenium


【解决方案1】:

在这些代码行中:

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++) {

【讨论】:

  • 错误:未知错误:元素 ... 在点 (675, 505) 处不可点击。其他元素会收到点击:
    ...
  • 但是我使用了循环,它在前五次迭代中运行,我使用了相同的元素。请帮忙!
  • 好吧,您是否尝试解决设计问题?显然在点 (675, 505) 有一个 div,而不是您的错误所述的链接。
  • 你能建议我任何修复吗??
  • 这是一个与您要求的完全不同的问题。它甚至可能不在您显示的代码中,而是在关联视图中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
  • 2023-02-01
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多