2017-02-03 26 views
2

enter image description here火狐 - 51.0.1 硒 - 3.0.5 的Ubuntu - 16.04(64位)無法抑制的Firefox彈出一個文件下載

這裏是我的代碼如下所示:

profile = Selenium::WebDriver::Firefox::Profile.new 
profile['browser.download.dir'] = "/tmp/webdriver-downloads" 
profile['browser.download.folderList'] = 2 
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf" 
profile['pdfjs.disabled'] = true 

driver = Selenium::WebDriver.for :firefox, :profile => profile 
driver.get "https://s3.amazonaws.com/shopsocially-misc/vfs/vfs_test_sample.csv" 

請運行上面的代碼,並看到彈出窗口。

這是我的錯誤:

Selenium::WebDriver::Error::UnknownError: Failed to decode response from marionette 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/response.rb:32:in `initialize' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/http/common.rb:85:in `new' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/http/common.rb:85:in `create_response' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/http/default.rb:107:in `request' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/http/common.rb:63:in `call' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/w3c_bridge.rb:640:in `raw_execute' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/w3c_bridge.rb:615:in `execute' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/remote/w3c_bridge.rb:126:in `get' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/common/navigation.rb:32:in `to' 
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.5/lib/selenium/webdriver/common/driver.rb:132:in `get' 
    from (irb):70 
    from /usr/bin/irb:11:in `<main>' 

據我試過了,似乎是一個問題與我現在用的是新版本。 如果我錯了,請糾正我。

+0

它不是一個彈出窗口,它是一個下載確認,並且您想要設置它,以便它永遠不會詢問是否保存。看看這個問題的答案:http://stackoverflow.com/questions/12759256/selenium-firefox-profile-for-saving-a-file(即'firefoxProfile.setPreference(「browser.helperApps.neverAsk.saveToDisk」,「 text/csv「);') –

+0

沒有工作。我添加了我獲得的對話框,當我點擊URL –

回答

3

MIME類型讓你失望。在你的例子中,你已經設置爲'application/pdf'。我已經用wget下載了該文件以確定MIME類型。

wget https://s3.amazonaws.com/shopsocially-misc/vfs/vfs_test_sample.csv 
    ... 
    Content-Type: application/octet-stream 
Length: 200 [application/octet-stream] 
... 

應用/八位字節流的內容類型意味着服務器本身doesn't know what kind of file this could be。由於Selenium明確定義了它將在browser.helperApps.neverAsk.saveToDisk中接受的MIME類型,因此導致您的失敗。

此配置文件將有助於自動下載不同類型的文件,包括application/octet-stream

# Create a firefox driver that can be passed to HeadlessBrowser.new 
def start_driver 
    profile = Selenium::WebDriver::Firefox::Profile.new 
    profile["browser.download.folderList"] = 2 # This allows downloads to be sent to a custom location 
    profile["browser.download.manager.showWhenStarting"] = false 
    profile["browser.download.dir"] = `/home/stefan/Downloads` # download to this custom path 

    # FILES WILL NOT DOWNLOAD UNLESS THEIR MIME TYPE IS INCLUDED IN THIS LIST! 
    profile["browser.helperApps.neverAsk.saveToDisk"] = accepted_mime_types_for_download 

    driver = Selenium::WebDriver.for :firefox, :profile => profile 
    return driver 
end 

def accepted_mime_types_for_download 
    [ 
    "application/vnd.ms-exceltext/csv", 
    "application/csv", 
    "application/zip", 
    "text/csv", 
    "application/x-msexcel", 
    "application/excel", 
    "application/x-excel", 
    "application/vnd.ms-excel", 
    "image/png", 
    "image/jpeg", 
    "text/html", 
    "text/plain", 
    "application/msword", 
    "application/xml", 
    "application/octet-stream" 
    ].join(",") 
end 
+1

謝謝....它的作品! –

+0

@TheRookie你可以考慮通過點擊答案旁邊的小+ 50按鈕來獎勵賞金。 – Cullub