如何使用Selenium Webdriver在Chrome中下载PDF文件

x9ybnkn6  于 2023-04-30  发布在  其他
关注(0)|答案(7)|浏览(331)

我想用selenium在chrome中下载pdf。

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")  
               + System.getProperty("file.separator")
               + "BrowserDrivers"
               + System.getProperty("file.separator")
               + "chromedriver.exe");

String downloadFilepath = "C:\\Users\\Vinod\\Downloads";

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);

//Save Chrome Opions
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);

driver = new ChromeDriver(cap);
driver.get(url);

我试过上面的代码,但它不工作

3wabscal

3wabscal1#

由于chrome57自动pdf预览不再作为插件工作,现在有一个设置你可以改变它。实际上,你可以通过查看chrome自己的首选项对话框来检查首选项的名称,在“内容设置”下的标志上写着“在默认的PDF查看器应用程序中打开PDF文件”。“

你可以将其设置为false以避免自动PDF预览,像这样(ruby示例):

caps = Selenium::WebDriver::Remote::Capabilities.chrome(
        "chromeOptions" => {           
            'args' => ['disable-gpu', "--window-size=1920,1080"],
            prefs: {
                "download.prompt_for_download": false,
                "download.directory_upgrade": true,
                "plugins.always_open_pdf_externally": true,
                "download.default_directory": DownloadHelpers::PATH.to_s
            }
        }
    )
Capybara::Selenium::Driver.new(
        app,
        browser: :chrome,
        desired_capabilities: caps
    )
anauzrmj

anauzrmj2#

以下是任何使用C#的人的选项。NET

var tsTimeout = new TimeSpan(0, 5, 0);

ChromeOptions chromeOptions = new ChromeOptions(); 
chromeOptions.AddUserProfilePreference("download.default_directory", _downloadFolder); 
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false); 
chromeOptions.AddUserProfilePreference("download.directory_upgrade", true); 
chromeOptions.AddUserProfilePreference("plugins.plugins_disabled", "Chrome PDF Viewer"); 
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

_driver = new ChromeDriver(CWebCrawler.WebCrawlerRootFolder, chromeOptions, tsTimeout);
yduiuuwa

yduiuuwa3#

您可以加载此页面并使用selenium导航更改设置:

chrome://settings/content/pdfDocuments
u0sqgete

u0sqgete4#

您需要在您的Chrome个人资料中添加以下声明:

chromePrefs.put("pdfjs.disabled", true);

看来,较新版本的浏览器来与内置的能力,显示PDF文件内的浏览器。更多信息请参考this,虽然它是firefox配置文件,但仍然是一个很好的阅读。
希望这能解决你的问题,否则你可能需要降级你的chrome才能让它工作。如果你有任何疑问请告诉我。

wqlqzqxt

wqlqzqxt5#

您是否使用Adobe Acrobat/Adobe Reader来显示PDF?如果是这样,那么您需要修改的可能是Abode的行为。
以下是我必须采取的步骤:
1.打开Adobe Reader
1.编辑菜单,首选项
1.从类别列表中选择Internet
1.取消选中在浏览器中显示PDF
1.按OK
您也可以在Chrome中禁用Adobe插件,这将强制Chrome下载PDF。
在chrome://plugins下。
但正如你说的,它的最新的Chrome浏览器,然后检查是否Chrome PDF视图可见插件,如果是的,也禁用它。

  • 禁用“Chrome PDF Viewer”或取消标记“始终允许运行”复选框尝试两者

cgvd09ve

cgvd09ve6#

下面的python代码在Chrome中通过禁用pdf查看器来下载pdf。

options = webdriver.ChromeOptions()
prefs = {"download.default_directory": chromeDownloadPath,
         "download.prompt_for_download": False,
         "download.extensions_to_open": "applications/pdf",
         "plugins.plugins_disabled": "Chrome PDF Viewer",
         "plugins.always_open_pdf_externally": True}
options.add_experimental_option("prefs", prefs)    
driver = webdriver.Chrome('chromedriver', options=options)
fhity93d

fhity93d7#

Obrigado,depois a ajuda de vcs meu código rodou da seguinte forma:

chrome_options = webdriver.ChromeOptions()
    prefs = {
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing.enabled": True,
        "disable-popup-blocking": True,
        "download.default_directory": chromeDownloadPath,
        "download.prompt_for_download": False,
        "download.extensions_to_open": "applications/pdf",
        "plugins.plugins_disabled": "Chrome PDF Viewer",
        "plugins.always_open_pdf_externally": True,
        "download.directory_upgrade": True,
        "safebrowsing.enabled": True,
        "disable-popup-blocking": True
        }

相关问题