【问题标题】:Download xml file from the website and stored in desired location (java / selinium with chrome browser)从网站下载 xml 文件并存储在所需位置(带有 chrome 浏览器的 java / selenium)
【发布时间】:2019-05-15 11:13:51
【问题描述】:

从网站下载 xml 文件并存储在所需位置(带有 chrome 浏览器的 java / selinium)

下面是html代码

<!-- Start of Code which handles XML Download-->
<a href="javascript:downloadXML()">
<img src="/img/tabs/downloadxml.gif" alt="Download" name="imag34" width="40" height="20" border="0">
</a>
<!-- End of Code which handles XML Download-->

首先,单击图像,文件应自动存储在我想要的位置

下面是我的 chrome 配置

File file = new File("resources/chromedriver.exe");
        String downloadFilepath = "mydownload path";
        Map<String, Object> preferences = new Hashtable<>();
        String absolutePath = file.getAbsolutePath();
        System.setProperty("webdriver.chrome.driver", absolutePath);

        preferences.put("profile.default_content_settings.popups", 0);
        preferences.put("download.prompt_for_download", "false");
        preferences.put("download.default_directory", downloadFilepath);

        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", preferences);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        driver = new ChromeDriver(capabilities);

执行

while clicking the image the xml file downloaded in my desired location with 0kb size

问题: 为什么文件没有完全下载?我的代码有什么错误吗?

【问题讨论】:

    标签: javascript java google-chrome selenium-webdriver selenium-chromedriver


    【解决方案1】:

    需要定义 MIME。尝试添加类似于下面的内容,用于处理 Excel 文件

     profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
                        "text/csv,application/x-msexcel,application/octet-stream");
     profile.setPreference("browser.helperApps.neverAsk.openFile",
                        "text/csv,application/x-msexcel,application/octet-stream");
    

    有关 MIME 类型的更多信息,请参阅https://www.whatismybrowser.com/detect/mime-types-supported

    xml 的 MIME 类型是 application/xml

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多