简体   繁体   中英

Selenium: Download file to specific folder using chrome driver

I am using selenium with chromedriver to automate a login page and download a file to particular folder. I am able to login and click on download link.But this downloads the file to default download folder. But i want the file to be downloaded to my desired folder. I used below code(vb.net). I got it from this link which is in C#

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

But it does not work.The file downloaded contains html source of page , but actual file from that url should be a pdf(when I manually navigate the url in the same chrome window I get pdf file downloaded) what I am doing wrong?I think handling the cookies may be wrong in cookieString function. Or is there any other method to download files to my required path after i login to page.

It would be easier to use File.Move to move your file from the default download folder to your desired folder

I am currently facing the same issue, and currently ChromeDrive cannot change the default download location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM