【问题标题】:How to set Safari download location - Selenium WebDriver如何设置 Safari 下载位置 - Selenium WebDriver
【发布时间】:2019-12-26 20:36:29
【问题描述】:

尝试将 safari(驱动程序)下载目录设置到特定位置。 现在它只会将文件下载到默认的“下载”文件夹。

String currentDirectory = System.getProperty("user.dir");
String downloadFilePath = currentDirectory+"/download/";

已经试过了:

dc.setCapability("safari.download.dir", downloadFilePath);
dc.setCapability("browser.download.dir", downloadFilePath);
dc.setCapability("safari.options.dataDir", downloadFilePath); // ("safari.options.dataDir" // this part won't work)

safariOptions.setCapability("safari.options.dataDir", downloadFilePath);

safariPrefs.put("download.deafult_directory", downloadFilePath); // this one I am using for chromedriver (chromePrefs)

【问题讨论】:

  • 你为downloadFilePath设置了什么值?
  • user.dir/download 所以它会保存在项目中。它适用于 chrome。
  • @RoyalJelly 你找到适合 Safari 的配置了吗?

标签: java selenium selenium-webdriver safari safaridriver


【解决方案1】:

好吧,您可以尝试使用 HTTP 连接来下载文件!这样,您肯定可以将其保存到变量中指定的目录中。

大多数语言都有用于执行 HTTP 请求的 API(或库)。例如,要在 Java 中完成此操作,您可以使用 URL.openConnection()

String link = linkElement.getAttribute("href");
URL url = new URL(link);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");

然后您可以使用 HttpURLConnection.getInputStream() 将文件内容写入您的首选位置。

try (InputStream in = httpURLConnection.getInputStream()) {
    Files.copy(in, new File("/path/to/file.ext").toPath(),
        StandardCopyOption.REPLACE_EXISTING);
}

仅在您使用 Cookie 的情况下: 您可以将它们添加到您的 HTTP 连接中。如果在这种情况下,您必须使用保存在 cookie 中的密码,这将非常有用。

Set<Cookie> cookies = webDriver.manager().getCookies();
String cookieString = "";

for (Cookie cookie : cookies) {
    cookieString += cookie.getName() + "=" + cookie.getValue() + ";";
}

httpURLConnection.addRequestProperty("Cookie", cookieString);

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多