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

时间:2018-12-14 07:41:57

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

从网站上下载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

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

1 个答案:

答案 0 :(得分:0)

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