无法抑制文件下载的 Firefox 弹出窗口
Unable to suppress a firefox pop-up for a file download
火狐 - 51.0.1
硒 - 3.0.5
Ubuntu - 16.04(64 位)
我的代码如下所示:
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>'
据我所知,我使用的新版本似乎有问题。
如有不妥请指正
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]
...
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
我的代码如下所示:
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>'
据我所知,我使用的新版本似乎有问题。 如有不妥请指正
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]
...
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