首页 > 编程知识 正文

selenium设定路径,seleniumwebdriver教程

时间:2023-05-06 12:14:00 阅读:208662 作者:1532

很多时候你需要从网站上下载不同种类的文件,像是MS Excel file, MS Word File, Zip file, PDF file, CSV file, Text file, 等等。
如果手动下载,会弹出一个保存文件的对话框。

现在,selenium webdriver还不能处理这样的对话框。不过,你可以用别的好的selenium webdriver的特点去很轻松的下载文件,而不用处理这个对话框。在了解这些特点之前,我们先来看看文件的MIME类型。

什么是文件的MIME

MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。
用selenium webdriver时, 你必须提供文件的MIME类型。有很多工具可以检测到文件的MIME类型。可以搜索MIME checker来找到合适的工具。
以下是5中常见的文件的MIME类型:
1. Text File (.txt) - text/plain
2. PDF File (.pdf) - application/pdf
3. CSV File (.csv) - text/csv
4. MS Excel File (.xlsx) - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
5. MS word File (.docx) - application/vnd.openxmlformats-officedocument.wordprocessingml.document
6. zip file(.zip) - application/zip

利用selenium webdriver下载不同类型的文件(pdf,txt等等)的代码实例 FirefoxProfile fprofile = new FirefoxProfile();//Set Location to store files after downloading.fprofile.setPreference("browser.download.dir", "D:\WebDriverdownloads");fprofile.setPreference("browser.download.folderList", 2);//fprofile.setPreference("browser.download.useDownloadDir", true);//fprofile.setPreference("browser.helperApps.alwaysAsk.force", false);fprofile.setPreference("browser.download.manager.showWhenStarting", false);fprofile.setPreference( "pdfjs.disabled", true );fprofile.setPreference( "browser.helperApps.neverAsk.saveToDisk", "application/zip,text/plain,application/vnd.ms-excel,text/csv,text/comma-separated-values,application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document");

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。