以下是该方案:
1)创建一个带有输入字符串的文件2015年9月2)将下拉列表收集到一个数组中3)如果数组等于字符串(在该文件中)来自循环其他下载新月报告并覆盖相同的带有新月份名称的文本文件。
我们在代码中尝试使用string,但没有创建文件(不知道如何将该txt文件链接到代码以检查数组是否等于字符串并且迭代必须发生)。
以下是代码:
driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
WebElement mSelectElement = driver.findElement(By.xpath("//select[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
List<WebElement> optionsList = mSelectElement.findElements(By.tagName("option"));
String oldMonth = "";
//if (optionsList.size() > 1) {
// Here considering first string from the optionsList(Here first
// string is 'All')
// String newMonth = optionsList.get(0).getText();
//}
for (int i = 2; i < optionsList.size(); i++) {
WebElement element = optionsList.get(i);
String newMonth = element.getText();
if (!oldMonth.equals("All") && !newMonth.equals("All")) {
if (newMonth.equals(oldMonth)) {
// IF the string are same, nthng we need to do
} else if (!newMonth.equals(oldMonth)) {
/*
* If the string are not same,then i.e., considered as new
* Month, download the new month details
*/
element.click();
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
Wait(200000);
//Click on File save button
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
//wait time to load the options
Wait(20000);
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
//fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
//fprofile.setPreference( "pdfjs.disabled", true );
Wait(10000);
// driver.manage().timeouts().implicitlyWait(10,
// TimeUnit.SECONDS);
System.out.println( "New month data downloaded in csv format:==>"+newMonth);
break; } }
oldMonth = newMonth;
} }
答案 0 :(得分:1)
首先,我建议使用Select而不是WebElement。
Select myDropdown = new Select(driver.findElement(By.xpath("//select[@id='ReportViewerControl_ctl04_ctl03_ddValue']")));
其次,我没有看到任何创建或读取文件的语句。你能告诉我代码吗?就个人而言,我更喜欢Apache CommonsIO。
FileInputStream in = new FileInputStream("month.txt");
try {
String oldMonth = IOUtils.toString(in);
} finally {
in.close();
}