2016-11-17 22 views
3

我在哪裏可以找到描述我可以在Selenium和Chrome瀏覽器中使用的選項的文檔?我想在Web瀏覽器中打開一個鏈接(以獲取憑據),但不要下載相應的文件(.pdf或.tiff或.jpeg)。我在Windows 7筆記本電腦上使用Python 2.7,selenium 3.0.1和Chrome Version 54.0.2840.99(和chromedriver.exe)。如何使用Chrome中的Selenium Python綁定來控制文件的下載

  # Chrome web browser 
      options = webdriver.ChromeOptions() 
      options.add_argument('--ignore-certificate-errors') 
      #options.add_argument('--disable-download-notification') #doesn't seems to work 
      #options.add_experimental_option("prefs", {"download.default_directory","C:\Users\xxx\downloads\Test"}) # doesn't work 
      #options.add_experimental_option("prefs", {"download.prompt_for_download": False}) # doesn't seems to work 
      #options.add_experimental_option("prefs", {'profile.default_content_settings': {'images': 2}})# this will disable image loading in the browser 
      options.add_argument("user-agent="+user_agent_profile) 
      driver_main = webdriver.Chrome(chrome_options=options) 

      # opening the web application portail 
      driver_main.get("https://my_link") 

我發現了很多關於這個主題的討論,但沒有一個解決方案的工作原理。例如:

add_experimental_option("prefs", {"download.default_directory","C:\Users\xxx\downloads\Test"}) 

不爲我

相同的工作:

add_experimental_option("prefs", {"download.prompt_for_download": False}) 

(我也有 「假」 試試)

同時:

add_argument("user-agent="+user_agent_profile) 

似乎工作!

我不知道要明白什麼是錯的

我的問題是,它開始給每個我打開一個文件名(1)文件中的鏈接時,下載文件(2)....文件(99)然後從100開始打開一個彈出窗口「另存爲」。所以我想要麼根本不下載文件,要麼可以將其移動到「回收站」中的特定文件夾中。

如何找到哪些選項可以與add_argument和add_argument一起使用?我想看看鉻://約/但我看不出有直接對應

非常感謝

乾杯

法比安斯基

+0

更改下載路徑,這一點 - ' 「download.default_directory」, 「C:\\ \\用戶\\ XXX \\下載測試」 '。它對我來說很有魅力。 –

回答

6

你申報的默認目錄的路徑無效。要麼逃避反斜線,要麼提供文字串。

options = webdriver.ChromeOptions() 
options.add_experimental_option("prefs", { 
    "download.default_directory": r"C:\Users\xxx\downloads\Test", 
    "download.prompt_for_download": False, 
    "download.directory_upgrade": True, 
    "safebrowsing.enabled": True 
}) 
driver = webdriver.Chrome(chrome_options=options) 

這裏是可用的選擇:

https://cs.chromium.org/chromium/src/chrome/common/pref_names.cc

+0

嗨弗洛朗,謝謝。你說對了,我給你的例子路徑錯了。正確的道路是行不通的,我想我找到了問題。我意識到,如果我查看Chrome網頁的設置,我可以看到我無權更改下載路徑(這是由我的compagny設置的!)。 **「download.prompt_for_download」的相同內容:False ** with ** True **或** False **我看到相同的東西。這可能是同一個問題,我並不是很想改變這個參數。謝謝 –

+0

你可以嘗試'options.add_argument('disable-component-cloud-policy')'看看它是否禁用了策略。 –

+0

嗨弗洛朗,我試了一下,沒有任何影響。我會嘗試使用這些限制的另一臺筆記本電腦,看看它是否有效。我設法通過在複製後刪除下載文件夾中的臨時文件來解決大部分問題,但.jpg會在瀏覽器中顯示並且有時會使系統崩潰。我試着玩.set_page_load_timeout(10)來避免這個問題。由於 –

相關問題