Selenium:使用chrome驱动程序将文件下载到特定文件夹

时间:2014-08-09 13:10:23

标签: vb.net selenium selenium-chromedriver

我正在使用chlenium和chromedriver自动化登录页面并将文件下载到特定文件夹。我可以登录并点击下载链接。但是这会将文件下载到默认下载文件夹。但我希望将文件下载到我想要的文件夹。 我使用下面的代码(vb.net)。我是从C {

中的this link得到的
Protected Function downloadFile(ByVal driver As IWebDriver, ByVal url As String, ByVal localPath As String)
    Try
        Dim client = New WebClient()
        client.Headers(HttpRequestHeader.Cookie) = cookieString(driver)
        client.DownloadFile(url, localPath)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Function

Private Function cookieString(ByVal driver As IWebDriver) As String
    Try

        Dim cookies = driver.Manage().Cookies.AllCookies
        Dim a As New System.Text.StringBuilder

        For index As Integer = 0 To cookies.Count - 1
            a.Append(cookies(index).Name)
            a.Append("=")
            a.Append(cookies(index).Value)
            a.Append("; ")

        Next
        cookieString = a.ToString

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Function

但它不起作用。下载的文件包含html页面源,但该网址的实际文件应该是pdf(当我手动浏览同一个chrome窗口中的url时,我得到pdf文件下载)我在做什么错了?我认为在cookieString函数中处理cookie可能是错误的。或者在登录页面后是否还有其他方法可以将文件下载到我所需的路径。

1 个答案:

答案 0 :(得分:0)

使用File.Move将文件从默认下载文件夹移动到所需文件夹会更容易

我目前面临同样的问题,目前ChromeDrive无法更改默认下载位置