我正试图使用java selenium下载一个文件。点击下载按钮,从网站启动和完成下载需要时间(下载时间取决于文件大小和网速)。
但是,下一个代码会在代码driver.findElement(By.xpath("//*[@id='downloadReport']/div")).click();
之后立即执行,而无需等待下载完成。
文件名是动态的(基于从Excel工作表SearchData.xls中提取的搜索数据的不同文件名
任何帮助都将不胜感激。
守则如下:
package Basic;
import java.awt.AWTException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class sftest2 {
public static void main(String[] args) throws IOException, InterruptedException, AWTException {
System.setProperty("webdriver.edge.driver", "D:\\Installer\\msedgedriver.exe");
File file = new File("D:\\SearchData\\SearchData.xls");
FileInputStream inputStream = new FileInputStream(file);
HSSFWorkbook wb=new HSSFWorkbook(inputStream);
HSSFSheet sheet=wb.getSheet("SF_NSF");
int rowCount=sheet.getLastRowNum()-sheet.getFirstRowNum();
WebDriver driver = new EdgeDriver();
driver.manage().window().maximize();
driver.get("https://******************");
driver.navigate().to("https://******************************");
for(int i=1;i<=rowCount;i++) {
driver.findElement(By.name("cAccount")).sendKeys("Search");
driver.findElement(By.xpath("//*[@id='abcSearchAction']/div[1]/div[3]/div[4]/img")).click();
WebElement bName=driver.findElement(By.id("bName"));
borrowerName.sendKeys(sheet.getRow(i).getCell(0).getStringCellValue());
driver.findElement(By.xpath("//*[@id='search-button']/ul/li[1]/div/input")).click();
Thread.sleep(2000);
driver.navigate().refresh();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(20));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id=\'load_projectTable\']")));
if(driver.getPageSource().contains("View 1 -"))
{
driver.findElement(By.xpath("//*[@id='downloadReport']/div")).click();
Thread.sleep(50000); // Doesnot want to wait for specified time, the next code should execute immediately after complete download of file called through above code
driver.findElement(By.xpath("//*[@id='footer-container']/div[3]/a")).click();
}
else
{
//driver.findElement(By.xpath("//*[@id='three-icons']/ul/li[3]/a/div")).click();
Calendar cal = Calendar.getInstance();
java.util.Date time = cal.getTime();
String timestamp = time.toString().replace(":", "").replace(" ", "");
System.out.println(time);
System.out.println(timestamp);
TakesScreenshot ts = (TakesScreenshot)driver;
File src=ts.getScreenshotAs(OutputType.FILE);
File trg=new File(".\\screenshots\\SF_1Cr_B_Search_"+timestamp+".png");
FileUtils.copyFile(src,trg);
//Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='footer-container']/div[3]/a")).click();
}
}
//Close the workbook
wb.close();
//Quit the driver
driver.quit();
}
}
1条答案
按热度按时间lymnna711#
您可以使用FluentWait来完成此操作,只需将downloadFolderPath值更改为下载文件夹并替换PrefixofFilename值