gpt4 book ai didi

java - 使用 Selenium 在 Chrome 浏览器中保存文档

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:38 25 4
gpt4 key购买 nike

我有一个 Selenium (Java) 测试应用程序,它可以在浏览器中打开 pdf 文档并将它们保存到硬盘驱动器以供稍后在测试中使用。由于测试将在多台机器上使用,因此无法手动设置配置文件;它必须以编程方式完成。

在 Firefox 中,我设置了配置文件首选项:

    FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", appSet.getDocumentSavePath());
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");
profile.setPreference("pref.downloads.disable_button.edit_actions", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
profile.setPreference("pdfjs.disabled", true);

Chrome 中是否有等效项?

我知道 Chrome 中有可以设置开关的功能,但我没有看到任何可以帮助我的功能。

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.binary", appSet.getChromeBinaryLocation());
capabilities.setCapability("chrome.switches", Arrays.asList("--allow-running-insecure- content=true"));

最佳答案

希望对大家有用。此解决方案适用于跨浏览器,并且在启动时不依赖于在浏览器中设置配置文件或首选项。

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie2;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.openqa.selenium.WebDriver;

public void downloadDocument(String documentName) {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);

CookieStore cookieStore = new BasicCookieStore();
Set<org.openqa.selenium.Cookie> cookies = driver.manage().getCookies();

for (org.openqa.selenium.Cookie cookie: cookies) {
BasicClientCookie2 cookie2 = new BasicClientCookie2(cookie.getName(), cookie.getValue());
cookie2.setAttribute(ClientCookie.VERSION_ATTR, "1");
cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
cookie2.setDomain(cookie.getDomain());
cookie2.setPath(cookie.getPath());
cookie2.setExpiryDate(cookie.getExpiry());
cookieStore.addCookie(cookie2);
}

HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

try {
HttpResponse httpResponse = httpClient.execute(new HttpGet(driver.getCurrentUrl()), httpContext);
InputStream inputStream = httpResponse.getEntity().getContent();

FileOutputStream fos = new FileOutputStream (getDocumentSavePath() + documentName.replaceAll("\\s+", "") + ".pdf");

byte[] buffer = new byte[2048];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}

fos.close();
} catch (ClientProtocolException e) { logger.error("message and trace")
} catch (IOException e) { logger.error("message and trace")
}

httpClient.getConnectionManager().shutdown();

}

关于java - 使用 Selenium 在 Chrome 浏览器中保存文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17337812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com