Selenium downloads PDF into default folder (Downloads) but not in specified in chromePrefs

huangapple go评论68阅读模式
英文:

Selenium downloads PDF into default folder (Downloads) but not in specified in chromePrefs

问题

使用Selenium Java在我的自动化框架中,并尝试从Chrome下载PDF,以下是我的代码:

System.setProperty("webdriver.chrome.driver", "resources/drivers/chromedriver.exe");

ChromeOptions options = new ChromeOptions();

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("plugins.always_open_pdf_externally", true);
chromePrefs.put("download.default_directory", "C:");
options.setExperimentalOption("prefs", chromePrefs);

driver = new ChromeDriver(options);

我指定了位置"C:"(仅用于测试目的),但问题是它将PDF下载到Downloads文件夹中。

此外,是否有办法指定我要下载的文件的名称?

英文:

Using Selenium Java in my automation framework and trying to download PDF from Chrome, below is my code:

        System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;resources/drivers/chromedriver.exe&quot;);
		
		ChromeOptions options = new ChromeOptions();
		
		HashMap&lt;String, Object&gt; chromePrefs = new HashMap&lt;String, Object&gt;();
        chromePrefs.put(&quot;profile.default_content_settings.popups&quot;, 0);
        chromePrefs.put(&quot;plugins.always_open_pdf_externally&quot;, true);
        chromePrefs.put(&quot;download.default_directory&quot;, &quot;C:&quot;);
        options.setExperimentalOption(&quot;prefs&quot;, chromePrefs);
        
		driver = new ChromeDriver(options);

I specified location "C:" (for just testing purpose) but the issue is that it downloads PDFs in Downloads folder.

Also is there a way to specify name of file that I'm trying to download?

答案1

得分: 1

我解决了这个问题,问题出在不推荐使用像 "C:"、"Desktop" 或相对路径 这样的文件夹,这就是为什么它不能正常工作。

英文:

I solved the problem, the issue was it is not recommended to use folders like: "C:" or "Desktop" or relative path, that's why it didn't work.

答案2

得分: 0

尝试这个,应该可以正常工作:

String desired_path = "D:\\user\\report";
HashMap hm = new HashMap();
hm.put("download.default_directory", desired_path);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", hm);
WebDriver driver = new ChromeDriver(options);
英文:

Try this, it should work :

String desired_path = &quot;D:\\user\\report&quot;;
HashMap hm = new HashMap();
hm.put(&quot;download.default_directory&quot;,desired_path);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(&quot;prefs&quot;,hm);
WebDriver driver = new ChromeDriver(options);

huangapple
  • 本文由 发表于 2020年9月11日 06:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63838272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定