【问题标题】:How to download POST Response file using selenium如何使用 selenium 下载 POST 响应文件
【发布时间】:2019-12-31 17:53:02
【问题描述】:

单击按钮时,我的应用程序将下载文件。 该文件将下载 POST 提交,在提交之前无法获取文件的 URL。

我正在寻找一种使用 selenium 下载 POST 请求附件的方法。有人可以指导我如何使用 selenium Java 实现这一目标吗?

【问题讨论】:

    标签: java html selenium pdf post


    【解决方案1】:

    我不确定您的确切用例,但这样的事情会起作用吗?我做了这个例子来自动下载 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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 2016-03-05
      • 2017-12-10
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多