【发布时间】: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 毫秒
【问题讨论】:
-
请阅读How to Ask,尤其是关于minimal reproducible example(MCVE)和How much research effort is expected?的部分,这将帮助您调试自己的程序并自己解决问题。如果您这样做并且仍然卡住,您可以返回并发布您的 MCVE、您尝试过的内容以及执行结果(包括任何错误消息),以便我们更好地帮助您。还提供指向页面和/或相关 HTML 的链接。