ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》高级技术之十一:Chrome浏览器_下载文件到指定的路径

2020-10-03 11:32:37  阅读:643  来源: 互联网

标签:VBA WD 浏览器 Service Chrome SeleniumBasic download Options


在Chrome浏览器上点击其他软件的安装包,或者超链接,通常会弹出一个文件保存对话框,必须手工选择文件夹和文件名称才能继续下去。

在SeleniumBasic中遇到这种情况会把程序堵死。

但是,Chrome浏览器的选项设置中,可以设置是否弹出文件保存对话框,以及保存文件的默认路径(如果不修改设置,默认位置是C:\Users\你的用户名\DownLoad)。

SeleniumBasic中的ChromeOptions对象,具有AddUserProfilePreference方法,可以添加个人偏好。

Private WD As SeleniumBasic.IWebDriver
Sub Download()
    On Error GoTo Err1
    Dim Service As SeleniumBasic.ChromeDriverService
    Dim Options As SeleniumBasic.ChromeOptions
    Set WD = New SeleniumBasic.IWebDriver
    Set Service = New SeleniumBasic.ChromeDriverService
    With Service
        .CreateDefaultService driverPath:="E:\Selenium\Drivers"
        .HideCommandPromptWindow = True
    End With
    Set Options = New SeleniumBasic.ChromeOptionsWith Options
        .BinaryLocation = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
        .AddUserProfilePreference "download.prompt_for_download", False
        .AddUserProfilePreference "download.default_directory", "D:\Temp"
        Debug.Print .ToString
    End With
    WD.New_ChromeDriver Service:=Service, Options:=Options
    WD.URL = "https://www.cnblogs.com/ryueifu-VBA/p/9482006.html"
    Dim file As SeleniumBasic.IWebElement
    Set file = WD.FindElementByLinkText("TreeviewEditor.rar")
    If file Is Nothing = False Then
        WD.ExecuteScript "arguments[0].scrollIntoView(true);", file
        file.Click
    End If
    WD.Quit
    Exit Sub
Err1:
    MsgBox Err.Description, vbCritical
End Sub

上述程序执行后,打印个人偏好,可以看到设置参数成功。

{
  "browserName": "chrome",
  "goog:chromeOptions": {
    "binary": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
    "prefs": {
      "download.prompt_for_download": false,
      "download.default_directory": "D:\\Temp"
    }
  }
}

本例的目的是下载一个压缩包,网址是https://www.cnblogs.com/ryueifu-VBA/p/9482006.html。

启动浏览器后自动导航到这个网址,然后根据超链接的文字进行定位,接下来直接Click,可以看到并未弹出保存对话框,而是直接保存到了D:\Temp。

 

标签:VBA,WD,浏览器,Service,Chrome,SeleniumBasic,download,Options
来源: https://www.cnblogs.com/ryueifu-VBA/p/13763779.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有