java selenide 等待文件下载完成示例代码
以下是一个使用Selenide等待文件下载完成的示例代码:
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import java.io.File;
import static com.codeborne.selenide.Condition.exist;
import static com.codeborne.selenide.Selenide.$;
public class FileDownloadExample {
public static void main(String[] args) {
// 设置Chrome浏览器驱动路径
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// 配置Selenide使用Chrome浏览器
Configuration.browser = "chrome";
// 打开网页
Selenide.open("https://www.example.com");
// 找到下载按钮并点击
SelenideElement downloadButton = $(By.cssSelector("button.download-button"));
downloadButton.click();
// 等待文件下载完成
File downloadedFile = Selenide.waitUntil(() ->
new File("path/to/downloaded/file").exists(), 10000);
// 验证文件是否存在
if (downloadedFile.exists()) {
System.out.println("文件下载完成!");
} else {
System.out.println("文件下载失败!");
}
}
}
在上面的示例代码中,我们使用了Selenide的waitUntil方法来等待文件下载完成。在等待过程中,我们检查目标文件是否存在。如果文件存在,我们打印出"文件下载完成!",否则打印出"文件下载失败!"。
请注意,您需要将path/to/chromedriver替换为您的Chrome驱动程序的实际路径,并将path/to/downloaded/file替换为您希望下载的文件的实际路径
原文地址: https://www.cveoy.top/t/topic/iQvR 著作权归作者所有。请勿转载和采集!