【发布时间】:2019-12-31 17:53:02
【问题描述】:
单击按钮时,我的应用程序将下载文件。 该文件将下载 POST 提交,在提交之前无法获取文件的 URL。
我正在寻找一种使用 selenium 下载 POST 请求附件的方法。有人可以指导我如何使用 selenium Java 实现这一目标吗?
【问题讨论】:
标签: java html selenium pdf post
单击按钮时,我的应用程序将下载文件。 该文件将下载 POST 提交,在提交之前无法获取文件的 URL。
我正在寻找一种使用 selenium 下载 POST 请求附件的方法。有人可以指导我如何使用 selenium Java 实现这一目标吗?
【问题讨论】:
标签: java html selenium pdf post
我不确定您的确切用例,但这样的事情会起作用吗?我做了这个例子来自动下载 Google Chrome.exe。与您的文件一样,也没有指向它的直接链接,而只是网站生成的下载。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.driver","where your ChromeDriver lives");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "where you want the file to be downloaded");
prefs.put("download.prompt_for_download", false);
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
String baseUrl = "https://www.google.com/chrome/";
driver.get(baseUrl);
try {
TimeUnit.SECONDS.sleep(5);
} catch(Exception e){
}
List<WebElement> targetsWithClass = driver.findElements(By.className("chr-cta__button--blue"));
targetsWithClass.get(1).click();
try {
TimeUnit.SECONDS.sleep(3);
} catch(Exception e){
}
driver.findElement(By.id("js-accept-install")).click();
}
}
【讨论】: