英文:
How to specify name of file being downloaded from chrome in Selenium Java testing framework
问题
使用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);
英文:
Using Selenium Java in my automation framework and trying to download PDF from Chrome but specify name of that file is there an existing option?
here 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);
答案1
得分: 0
在这个主题 https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading 中找到了解决方案,@supputuri 给出了答案。
无法为下载的文件指定名称,但以下是如何获取下载文件名称的代码。
public String waitUntilDonwloadCompleted(WebDriver driver) throws InterruptedException {
// 存储当前窗口句柄
String mainWindow = driver.getWindowHandle();
// 打开一个新标签页
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.open()");
// 切换到新标签页
// 切换到新打开的窗口
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// 导航到 Chrome 下载页
driver.get("chrome://downloads");
JavascriptExecutor js1 = (JavascriptExecutor)driver;
// 等待文件下载完成
Long percentage = (long) 0;
while (percentage != 100) {
try {
percentage = (Long) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value");
} catch (Exception e) {
// 无事可做,只需等待
}
Thread.sleep(1000);
}
// 获取最新下载的文件名
String fileName = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text");
// 获取最新下载的文件 URL
String sourceURL = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').href");
// 文件下载位置
String donwloadedAt = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div.is-active.focus-row-active #file-icon-wrapper img').src");
System.out.println("Download details");
System.out.println("File Name: " + fileName);
System.out.println("Downloaded path: " + donwloadedAt);
System.out.println("Downloaded from URL: " + sourceURL);
// 打印详情
System.out.println(fileName);
System.out.println(sourceURL);
// 关闭下载标签页
driver.close();
// 切换回主窗口
driver.switchTo().window(mainWindow);
return fileName;
}
英文:
Found solution in this topic https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading answer from @supputuri
It is not possible to give the name to downloaded file, but below is the code how to get the name of downloaded file.
public String waitUntilDonwloadCompleted(WebDriver driver) throws InterruptedException {
// Store the current window handle
String mainWindow = driver.getWindowHandle();
// open a new tab
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.open()");
// switch to new tab
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// navigate to chrome downloads
driver.get("chrome://downloads");
JavascriptExecutor js1 = (JavascriptExecutor)driver;
// wait until the file is downloaded
Long percentage = (long) 0;
while ( percentage!= 100) {
try {
percentage = (Long) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value");
//System.out.println(percentage);
}catch (Exception e) {
// Nothing to do just wait
}
Thread.sleep(1000);
}
// get the latest downloaded file name
String fileName = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text");
// get the latest downloaded file url
String sourceURL = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').href");
// file downloaded location
String donwloadedAt = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div.is-active.focus-row-active #file-icon-wrapper img').src");
System.out.println("Download deatils");
System.out.println("File Name :-" + fileName);
System.out.println("Donwloaded path :- " + donwloadedAt);
System.out.println("Downloaded from url :- " + sourceURL);
// print the details
System.out.println(fileName);
System.out.println(sourceURL);
// close the downloads tab2
driver.close();
// switch back to main window
driver.switchTo().window(mainWindow);
return fileName;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论