Is there any way to change chrome download folder path without closing the chrome?
Dim bot As New ChromeDriver
bot.SetPreference "download.default_directory", "c:\temp"
bot.SetPreference "download.directory_upgrade", True
bot.SetPreference "download.prompt_for_download", False
bot.Get "http://google.com"
bot.Quit
CodePudding user response:
I am assuming you are little bit confused about chrome browser download folder location and chrome driver download location.
Chrome browser:
You have Chrome browser in your machine and let's say its download folder location is
C:/Download/
If you run your selenium script to download some file by default the chrome driver will use download location as
C:/Download/
Chrome driver:
In your code, you changed the default location from
C:/Download/
toC:/Custom/
by usingdownload.default_directory
preference then the files be downloaded to the new location.Changing of download file location is applicable for chrome driver only not for chrome browser. Your chrome browser default location stays as
C:/Download/
In below example, I downloaded a file by changing the download folder location.
Code:
Dim bot As New ChromeDriver
bot.SetPreference "download.default_directory", "C:\Users\Nandan\Desktop"
bot.SetPreference "download.directory_upgrade", True
bot.SetPreference "download.prompt_for_download", False
bot.Get "https://www.hkex.com.hk/Market-Data/Securities-Prices/Equities/Equities-Quote?sym=700&sc_lang=en"
bot.FindElementByLinkText("Export to Excel").Click
bot.Timeouts.Server = 5000
End Sub
Output:
CodePudding user response:
this was my entire code I want to change the download directory after 1 st looop was done. but not changing.
Sub pathcheck()
Dim bot As WebDriver
Set bot = New ChromeDriver
Dim J As Integer
Dim myfilepath As String
Dim path As String
Dim lastrow As Long
lastrow = Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Const URL = "https://ibgvinternational.neeyamo.com/Login.aspx"
bot.Start "Chrome"
bot.Get URL
bot.FindElementById("LoginNameText").SendKeys "asdf"
bot.FindElementById("LoginButton").Click
bot.FindElementById("PasswordText").SendKeys "fassd"
bot.FindElementById("LoginButton").Click
For J = 2 To lastrow
Worksheets("sheet1").Range("G1").Value = Worksheets("sheet1").Range("A" & J).Value
myfilepath = Worksheets("Sheet1").Range("F1").Value
MkDir myfilepath & Worksheets("Sheet1").Range("G1").Value
path = myfilepath & Worksheets("Sheet1").Range("G1").Value
bot.SetPreference "download.default_directory", path
bot.SetPreference "download.directory_upgrade", True
bot.SetPreference "download.prompt_for_download", False
bot.FindElementByXPath("//*[@id=""ctl00_Menu1""]/ul/li[2]/a").ClickAndHold
bot.FindElementByXPath("//*[@id=""ctl00_Menu1:submenu:3""]/li[2]/a").Click
bot.FindElementByXPath("//*[@id=""ctl00_ContentPlaceHolder1_txtCaseRefNo""]").Click
bot.FindElementByXPath("//*@id=""ctl00_ContentPlaceHolder1_txtCaseRefNo""]").SendKeys
Worksheets("Sheet1").Range("G1").Value
bot.FindElementById("ctl00_ContentPlaceHolder1_btnSearch").Click
bot.FindElementById("ctl00_ContentPlaceHolder1_gvDocument_ctl02_lnkDownload").Click
bot.FindElementById("ctl00_ContentPlaceHolder1_gvDocument_ctl03_lnkDownload").Click
bot.FindElementById("ctl00_ContentPlaceHolder1_gvDocument_ctl04_lnkDownload").Click
Next J
End Sub