我正在执行一个java selenium项目..
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", downloadLoc.getAbsolutePath());
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
firefoxProfile.setPreference("network.http.response.timeout", timeLimit);
// firefoxProfile.setPreference("network.http.pipelining.read-timeout", 1);
System.out.println("Total number of threads in before driver fn" + Thread.activeCount());
FirefoxDriver driver1 = new FirefoxDriver(firefoxProfile);
System.out.println("Total number of threads in after driver fn" + Thread.activeCount());
执行此操作后,它会创建4个线程 但我需要只使用一个线程来运行程序。因为在我的程序中我需要同步......它应该一步一步地去。由于这4个线程是不可能的。
在指定的类中,我有一个方法,前面是@Test 所以我试着让方法同步,但它没有用......因为它说移动文件时某些其他进程使用了像资源这样的东西。
所以我需要只使用一个线程来运行程序。 请给我一个很好的解决方案.. 如果您需要更多说明,请告诉我
添加完整代码
public void waitUntilDownloadCompletes(FirefoxDriver driver) {
waitTime = 0;
clicked = true;
System.out.println("Parent" + parentHandle);
while (true) {
System.out.println("waiting-----active count:" + Thread.activeCount() + "and time=" + waitTime);;
try {
createRowExcel(path, reportName, "Downloading is in progress- " + (waitTime / 60000) + " minutes" + ((waitTime / 1000) % 60) + " seconds", errorMessage, false);
// // updateColumn(updateExcelRowNum, updateExcelColNum, (waitTime / 1000) + "s (in progress)");
} catch (Exception e) {
}
System.out.println("wait time" + waitTime + "time limit" + this.timeLimit);
if (waitTime > timeLimit) {
try {
if (closeTheChildWindow(driver)) {
try {
System.out.println("HTML" + html);
createRowExcel(path, reportName, "Rejected Download after waiting " + (waitTime / 60000) + " minutes " + ((waitTime / 1000) % 60) + " seconds", errorMessage, true);
errorMessage = "";
// // updateColumn(updateExcelRowNum, updateExcelColNum, "Rejected Downloading after waiting " + (timeLimit / 60000) + " minutes");
// // resetCounters();
} catch (Exception e) {
}
break;
}
} catch (Exception e) {
driver.switchTo().window(parentHandle);
}
}
System.out.println("Total CSV " + totalNumberOfCSV + "Time in seconds" + (waitTime / 1000));
totalNumberOfCSV = -1;
if (totalCSV() > totalNumberOfCSV) {
System.out.println("Csv greater than existing");
if (!isPartialFileExist()) {
System.out.println("Downloaded successfully" + totalCSV());
totalNumberOfCSV = totalCSV();
driver.switchTo().window(parentHandle);
clicked = false;
timeCounter = 0;
break;
}
System.out.println("Partial file exists" + totalCSV());
}
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
driver.switchTo().window(parentHandle);
System.out.println("Error" + e);
}
waitTime += 1000;
}
breakWhile = false;
}
答案 0 :(得分:0)
无论如何,浏览器都将与selenium异步。
您必须使用WebDriverWait类使selenium等待加载页面和数据可用。例如,要等到按钮加载并可见:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='button1']")));