Disable file downloads Selenium Firefox - selenium

I'm building a webscraping app and I'm using Selenium with a Firefox driver to open my pages. Whenever it opens a link that leads to a download, my app stalls and hangs on the link for eternity.
I've tried looking for a solution, but they never mention disabling downloads. They only talk about enabling them, changing the download directory,...
Would it be possible to detect that the link is a download link and just skip it, or maybe to skip the link whenever it opens?

I had a slightly different problem and I solved it by changing Firefox capabilities.
Open about:config in Firefox to see list of them. Then you can pass them to webdriver through desired_capabilities.
caps = {'acceptInsecureCerts': True,
'browserName': 'firefox',
'marionette': True,
'moz:firefoxOptions': {
'args': ['--no-remote'],
'prefs': {
'browser.safebrowsing.downloads.enabled': False
}
},
driver = webdriver.Remote([...]
desired_capabilities=caps)

Related

Running Chrome from PortableApps with Selenium WebDriver fails

I've got C# Selenium project and want to run portable Chrome downloaded from here.
My chromeOptions are:
chromeOptions.BinaryLocation = Path.GetFullPath("C:\", "tests.browsers", "windows", "chrome", "GoogleChromePortable.exe"));
chromeOptions.AddArgument("--enable-logging");
chromeOptions.AddArgument("--v=1");
chromeOptions.AddExtension(Path.Combine("path", "to", "kontur-plugin.crx"));
But when I run the test, Chrome opens and shows this:
(notifications say "Failed to login %EXTENSIONNAME%, click to load it once again)
chrome_debug.log contains no errors.
What am I doing wrong? How can I figure out what is the problem with?
this might help you - Stackoverflow post
also, your CRX file should add as a file, not just the path.

Unable to download a file when chromedriver is in headless mode. How to get it work?

During the test, a file (.html) will be downloaded from the web application & I have to verify that file by opening it on the browser. In the non-headless mode, my test is working fine. But whenever I'm going to headless mode, that file is not getting downloaded to the download path (i.e. pointed in the "user.dir"). My chrome driver version is 2.44.609538 & selenium version is 3.14.
Apparently this could help you
Shawn Button post the answer related with it.
Downloading with chrome headless and selenium
Are you running the test from the command line?
Because, according to an answer to this question and this, when you run it via command line, your user.dir corresponds to your global user directory (C:\users\username).
This worked for our ruby implementation:
Capybara.register_driver :scrapping_driver do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=1366,2000')
options.add_preference(:download, directory_upgrade: true,
prompt_for_download: false,
default_directory: "#{Rails.root}/")
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
Selenium::WebDriver::Service.driver_path = Webdrivers::Chromedriver.driver_path
driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Pay attention at download behaviour
I met same situation.
Headless mode is very faster. So your code might be implemented to detect download(DL).
Solution.
(Current your process?) .click for DL -> Detecting download file generation.
(My solution) Detecting download file generation -> .click for DL.
I implemented the above mechanism using callback function.

How to download pdf from a website through selenium automation using Chrome 63.0.3239.108

I want to download pdf from a website through selenium automation. I am using Chrome 63.0.3239.108 version. I tried below 2 code snippets. but those are not working.
disabling chrome extensions by setting preference like below.
preferences.put("plugins.plugins_disabled", [
"Adobe Flash Player",
"Chrome PDF Viewer"
])
another preference that i used
preferences.put("plugins.always_open_pdf_externally",true);
preferences.put("pdfjs.disabled", true);
Both didn't help to accomplish.Is there any thing to try.
the way I have downloaded files in the past using chromedriver is forcing the chromedriver to download the pdf's in a directory that i can read from
see if the below approach works. This is using python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"<directory_path>",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})

Nightwatch tests do not find elements in Headless chrome mode

I am trying to get my tests (written in Nightwatch.js) to run locally in headless chrome.
However, the tests fail since they are not able to find the elements in headless mode (they work without headless mode though).
If I check the failure screenshots I only get a white screen.
But if test checks for the "body" element, it actually pass. So I think the page is loaded, but maybe headless chrome, for some reason, cannot load the javascript? Later I wait for divs and buttons etc to be visible for several seconds, but it does not find them.
Do you have any ideas what could be wrong?
I have added the --headless and --disable-gpu flags in desiredCapabilities in the nightwatch config file.
So, as I said in my reply I had the same issue yesterday, I was just trying to do a dummy test on google homepage. This morning with a fresh brain I tried to tackle that. I had the brilliant idea to take a screenshot just before nightwatch fails to find an element.
It turns out the "normal" chrome had the home page in English, and the "headless" chrome had the homepage in french, for some reason.
I found this: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1925 may be explaining why.
The workaround I found to always have the correct language is:
"chromeOptions" : {
"args" : ["--lang=fr", "--headless"]
}
I still have a hard time setting it in english (weirdly) but I hope this will save someone a couple of hours in the future
I think you should declare binary path in Nightwatch.js
If you are on linux please try this, it works perfectly for me :
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"chromeOptions": {
"args": [
"headless", "disable-gpu"
],
"binary": "/usr/bin/google-chrome"
}
}
If you are on Mac, replace your binary path, eg /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome

Run parallel Protractor tests in multiple Chrome tabs

I'm using the following .conf:
capabilities: {
browserName : 'chrome',
shardTestFiles: true,
'time-zone': 'AEST',
maxInstances: 5,
singleWindow: true
}
I'm using singleWindow as described in the capabilities page for Selenium and the properties are passed to the WebDriver according to this. However I'm still seeing multiple individual windows appearing and disappearing during execution. These fight for focus and it's very annoying while doing other work.
Is it possible to run parallel tests in separate tabs rather than separate instances/windows of Chrome?