2017-05-24 310 views
2

使用Win7,Office2010,Chrome 58和Selenium 2.0.9,我自動從幾個網站下載一些文件(注意,我實際上需要文件和網站不是我的 - 我是而不是測試我自己的網站),並且我想控制文件最終被下載的位置。如何使用Selenium和VBA設置Chrome下載目錄

我看過相當多的搜索結果,並且我發現的所有內容都讓我看到以下版本的代碼,每個Driver.SetPreference變體已經過獨立測試,沒有一個可以工作。

Private Sub DownloadDirTest() 

    Dim Driver As Selenium.ChromeDriver 
    Set Driver = New Selenium.ChromeDriver 

    Driver.SetPreference "browser.download.dir", "\\server\share\my\long\path" 
    Driver.SetPreference "browser.download.dir", "\\\\server\\share\\my\\long\path" 
    'after mapping Y: to "\\server\share\my\long\path" in Windows Explorer 
    Driver.SetPreference "browser.default_directory", "Y:\" 
    Driver.SetPreference "browser.download.dir", "c:\" 
    Driver.SetPreference "browser.default_directory", "c:\" 

    Driver.Start "Chrome", "http://google.com" 

    Driver.Close 

End Sub 

正如已經在這裏看到:

enter image description here

大多數我見過的引用是爲Python,Java或Ruby的,他們都提及類似:

ChromeOptions options = new ChromeOptions(); 
options.addArguments("user-data-dir=/path/to/your/custom/profile"); 

似乎沒有任何等效的ChromeOptions可用於VBA。

1)有誰知道實際的屬性名稱來正確設置下載目錄?

2)我不是特別執着於Chrome瀏覽器,雖然它似乎快於IEDriver(在我的初步測試),而我不能讓當前的Firefox司機工作。如果有人指出如何在其他瀏覽器中可靠地設置DL目錄(以及指向更新的Firefox驅動程序的鏈接 - 我無法通過半心半意的搜索找到它),我願意使用那些。

回答

1

您示例中的首選項特定於Firefox。您需要設置針對Chrome的那些改變下載目錄:

Dim driver As New ChromeDriver 

driver.SetPreference "download.default_directory", "c:\temp" 
driver.SetPreference "download.directory_upgrade", True 
driver.SetPreference "download.prompt_for_download", False 
driver.SetPreference "safebrowsing.enabled", True 
driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer") 

driver.Get "http://google.com" 


driver.Quit 
+0

哇...說說缺少樹木,不見森林......你的[SeleniumBasic]的florentbr(https://開頭的github .com/florentbr/SeleniumBasic)成名?如果是這樣,我很榮幸!此外,該項目的工作還在繼續 - 最近似乎沒有太多活動。 – FreeMan

+1

該項目還活着。我將很快發佈新版本,以符合新的Selenium 3 API並支持Firefox的新驅動程序。 –

+0

壯觀!非常高興聽到它。 – FreeMan

相關問題