如何在Selenium Java测试框架中指定从Chrome下载的文件的名称

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

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(&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);

答案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(&quot;window.open()&quot;);
// switch to new tab
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// navigate to chrome downloads
driver.get(&quot;chrome://downloads&quot;);
JavascriptExecutor js1 = (JavascriptExecutor)driver;
// wait until the file is downloaded
Long percentage = (long) 0;
while ( percentage!= 100) {
try {
percentage = (Long) js1.executeScript(&quot;return document.querySelector(&#39;downloads-manager&#39;).shadowRoot.querySelector(&#39;#downloadsList downloads-item&#39;).shadowRoot.querySelector(&#39;#progress&#39;).value&quot;);
//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(&quot;return document.querySelector(&#39;downloads-manager&#39;).shadowRoot.querySelector(&#39;#downloadsList downloads-item&#39;).shadowRoot.querySelector(&#39;div#content #file-link&#39;).text&quot;);
// get the latest downloaded file url
String sourceURL = (String) js1.executeScript(&quot;return document.querySelector(&#39;downloads-manager&#39;).shadowRoot.querySelector(&#39;#downloadsList downloads-item&#39;).shadowRoot.querySelector(&#39;div#content #file-link&#39;).href&quot;);
// file downloaded location
String donwloadedAt = (String) js1.executeScript(&quot;return document.querySelector(&#39;downloads-manager&#39;).shadowRoot.querySelector(&#39;#downloadsList downloads-item&#39;).shadowRoot.querySelector(&#39;div.is-active.focus-row-active #file-icon-wrapper img&#39;).src&quot;);
System.out.println(&quot;Download deatils&quot;);
System.out.println(&quot;File Name :-&quot; + fileName);
System.out.println(&quot;Donwloaded path :- &quot; + donwloadedAt);
System.out.println(&quot;Downloaded from url :- &quot; + 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;

}

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

发表评论

匿名网友

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

确定