英文:
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("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);
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 = "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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论