Downloading PDF's with Webdriver - macos

I am trying to download pdfs with selenium webdriver with python bindings on OS X 10.8.
I actually need the pdf file, not just check if it the download link works. As I understand it, I need to set the firefox profile to download the pdf content type, rather than 'preview' which is the default.
My code to open an instance of firefox is:
def Engage():
print "Start Up FIREFOX"
## Create a new instance of the Firefox driver
profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', os.path.expanduser("~/Documents/PYTHON/Download_Files/tmp/"))
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/pdf'))
driver = webdriver.Firefox(firefox_profile=profile)
return driver
I have also tried initially setting the profile as :
profile = webdriver.FirefoxProfile()
## replacing :: profile = webdriver.firefox.firefox_profile.FirefoxProfile()
## the other attributes remained
This has the same results
This profile opens the pdf in preview mode in a new window, rather than download it.
I double checked the content type through requests and was able to confirm it as "application/pdf":
import requests
print requests.head('mywebsite.com').headers['content-type']
Any idea of what I am doing wrong?

Was facing a similar situation sometime back. Solution is quite easy. By default the settings in firefox opens pdf files rather than allowing you to download it. To overcome this type config:about in the browser and type pdfjs.disabled double click on the option. The value should change from false to true. Restart the browser and try opening any pdf file. It will download the file instead of opening it in the browser. Happy coding.

I was faced with the same problem. My code is in Java, but I'm sure you can transfer the properties to match in python. Here is what worked or me (note you need to specify a path to a directory that you can write to):
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( "browser.download.folderList", 2 );
profile.setPreference( "browser.download.dir", <YOUR DOWNLOAD PATH> );
profile.setPreference( "plugin.disable_full_page_plugin_for_types", "application/pdf" );
profile.setPreference(
"browser.helperApps.neverAsk.saveToDisk",
"application/csv,text/csv,application/pdfss, application/excel" );
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "pdfjs.disabled", true );

Related

Disable Firefox auto update requests

Hope I'm writing that in the relevant platform..
I'm working on a script that analyzing requests while browsing sites with Firefox.
I always see requests to aus4.mozilla.org, even after I changed the "Auto-Update" option in the browser to 'Disable' mode.
Does anybody faced with this problem before? How can I turn it off?
Here is how you can turn off Auto Updates on Firefox from Windows:
Run Regedit in windows (Must have admin right)
Expand HKEY_LOCAL_MACHINE > SOFTWARE > Policies
If the 2 folders Mozilla and Firefox don't exist under 'Policies'. Create them as per the following:
-Right click on "Policies" folder, Select New > Key then enter "Mozilla"
-Right click on "Mozilla" folder, Select New > Key then enter "Firefox"
-Right click on "Firefox" folder, Select New > DWORD (32 bit) value then enter "DisableAppUpdate"
-Double-click on "DisableAppUpdate" to modify Value data to 1 (hexadecimal). This will stop mozilla from downloading and updating Firefox;
It will show "Updates disabled by your system administrator"
To allow Firefox to update again; Modify the Value Data to 0 .
Weird, the option should work.
Double check that the app.update.auto and app.update.enabled configuration options are set to false (you can do that from about:config).
I would suggest using a better method to automate Firefox, for example Selenium. There are bindings for many programming languages, e.g. for Node.js.
Nowadays (65.0.2) disabling updates is relatively complicated. Here's the Windows solution:
In the firefox.exe directory, create one named distribution.
Inside distribution, place a filed named policies.json containing the following:
{
"policies": {
"DisableAppUpdate": true
}
}
Sources:
https://support.mozilla.org/en-US/questions/1232918
https://github.com/mozilla/policy-templates/blob/master/README.md

How to make the settings for Download in Firefox when launched using Webdriver?

I have set this option in my Firefox for Downloads. But each time I am opening the Firefox using Webdriver it is set to Save files to Downloads. So whenever there is a file which is to be downloaded from Firefox launched by Webdriver, it will save to default location.
What can I do so that it always asks me for Download location when launched using webdriver?
Create a firefox profile like this:
https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles
In this profile, set the correct download destination.Now in your code when you create the firefox driver, start it with the newly created profile. This should do the trick.
I was able to achieve this using the following code snippet:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.download.useDownloadDir", false);
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://www.google.com");

How to make Firefox open all links opened via WebDriver in the same window?

I want to open all links in the same window instead in new window.
I tried
profile.setPreference("browser.link.open_newwindow", 1)
but the result is:
WARNING: traffic.loop 0 error: Preference browser.link.open_external may not be overridden: frozen value=2, requested value=1
Is there an another way to open the links in the same window ?
You should modify the firefox profile parameters:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_newwindow.restriction", 0)
driver = webdriver.Firefox(firefox_profile=profile)
if this methode does not work, you can set perference using firefox Options:
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_preference("browser.link.open_newwindow.restriction", 0)
opts.set_preference("browser.link.open_newwindow", 3)
driver = webdriver.Firefox(firefox_options=opts)
(A) browser.link.open_newwindow - for links in Firefox tabs :
3 : divert new window to a new tab (default)
2 : allow link to open a new window
1 : force new window into same tab
(B) browser.link.open_newwindow.restriction - for links in Firefox tabs
0 : apply the setting under (A) to ALL new windows (even script windows)
2 : apply the setting under (A) to normal windows, but NOT to script windows
with features (default)
1 : override the setting under (A) and always use new windows
I've found a workaround!
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "document.getElementById('yourFormOrAnchorId').target=''";
js.executeScript(script);
After that you can select your anchor or any of the form elements and click or submit it. The target page will open in the same tab.
This basically changes the current HTML page so that anchors and forms don't force the browser to open new tabs or windows. For testing this might be suboptimal, but it simplifies the writing of tests a lot.
Try this out...
Modify FireFox profile parameters "browser.link.open_newwindow.restriction" and "browser.link.open_newwindow".
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.link.open_newwindow.restriction", 0);
profile.setPreference("browser.link.open_newwindow", 1);
If you are using Google Chrome then simply install this extension and it will take care of the rest of the task. This extension is also handy to open pop-ups in new tabs which usually opens in new windows. (First you need to download the extension .crx file from given location.)
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
In the selenium config file:
C:\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver_prefs.json
change the following line from:
"browser.link.open_newwindow": 2,
to:
"browser.link.open_newwindow": 3,
I test it and it worked
According to Selium docs (https://code.google.com/p/selenium/wiki/FirefoxDriver) the following property webdriver.firefox.profile controls the firefox profile used.
Which is where firefox gets the browser.link.open_newwindow on start up from. To create a new profile for your tests you can follow the instructions here https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles detailed configuration of the profile can be done either by editing the profile's pref.js or firing up the profile and editing it via about:config.
hope this of help!
Actually, Selenium is not responsible of the page opens in a new window or in a same window. It is fully depends upon the Browser settings which you used for execution.
For a sake take Firefox browser
If you want to open all the links in a new window. Do these steps
Open Tools
Click Options
Click Tabs menu
Check the box of Open new windows in a new tab instead.
Now click the link which opens a window. It will opens in a new tab of same window.

How to suppress the "checking compatibility of add-ons" dialog in Firefox with Selenium?

Since updated to Firefox 12, every time I launch Firefox with a particular profile with Selenium (in python and Mac OS 10.7) it pops up the "checking compatibility of add-ons" dialog, and sometimes this dialog would stay up forever and I have to force-quit it. After forcing quit it, a new instance of the Firefox would continue to launch and finishes the rest of the Selenium script successfully though.
I have tried setting extensions.checkCompatibility to false. This fixed it if I launched Firefox normally, but not if I launch it with Selenium. Any idea on how to suppress this dialog? Thanks!
This dialog is shown only once whenever Firefox is updated. The reason it is shown each time for you is probably that Selenium creates a new profile each time. If you set extensions.lastAppVersion preference to "12.0" (or whatever the current Firefox version is) then Firefox will no longer think that it has been updated and won't show this dialog. However, it should be easier to add a extensions.showMismatchUI preference and set it to false, this will suppress this dialog (but not the other upgrade actions).
Side-note: extensions.checkCompatibility preference no longer does anything starting with Firefox 3.6, it is a version-specific preference in the current Firefox versions. So you would have to set extensions.checkCompatibility.12.0 preference instead. That disables compatibility checking for extensions completely however, not just the dialog you are concerned about.
I have tried setting extensions.checkCompatibility to false. This fixed it if I launched Firefox normally, but not if I launch it with Selenium.
The reason it won't when you launch it with Selenium is the Firefox Driver will create a temporary profile in the temporary files directory, slowing down tests and taking up unnecessary space.
Create a profile for your test purposes and set what you need. Full instructions to create the SeleniumProfile can be found at https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles
In Java I have the following:
protected WebDriver createFirefoxDriver() {
File proFile = new File("C:\\Users\\<username>\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\xxxxxx42.SeleniumProfile");
FirefoxProfile ffProfile = new FirefoxProfile(proFile);
WebDriver ffDriver = new FirefoxDriver(ffProfile);
return ffDriver;
}
Do this to remove the "checking for addon's compatibility" Dialog. This is based on the Windows operating system..
Create a temporary FF Profile and start the server with that profileas shown below.
java -jar selenium-server-x.x.x.jar -firefoxProfileTemplate "/path/to/the/temp/profile"
Now use the following code.
import com.thoughtworks.selenium.*;
public class Test {
public static void main(String ar[]) {
Selenium sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
sel.start();
}
}
Now in the Run command type "%TEMP%" and you can see there a folder with same name as the selenium session. Copy the folder contents and replace them with your temp profile Contents.
Follow the steps below to remove the Addons compatibility.
1 . Create a new FF Profile
2 . Set the FF Profile as per required settings
3 . Just run a sample program of selenium such that it invokes firefox.
4 . Now you can find a folder with the same name as Selenium Session created somewhere in your sytsem. ( Most probably in the directory where the Temporary Content is saved)
5 . Copy the folder contents and replace them with the newly created profile.
Now you can use the newly created profile whenever required. Whenever FF is updated , always check whether the existing addons are compatible with the Existing version by once invoking firefox with the Profile.

Using firefox and Chrome Driver in selenium 2.24.0

I'm using Selenium to buid a project about automation testing but I have some problem with these lines of code :
String exePath = "Browser\\firefox5.0.1\\firefox.exe";
var firefoxProfile = new FirefoxProfile();
m_browser = new FirefoxDriver(new FirefoxBinary(exePath), firefoxProfile);
m_browser.Navigate().GoToUrl(url);
I can't set firefox url with these codes. But if I use InternetExplorer instead, I can set IE url. I don't know where the problem is
Plz help me. Thank you so much!
If you want to use the chrome driver , you will have to set a property as follows -
System.setProperty("webdriver.chrome.driver","chrome driver path");
You can download the chromedriver.exe for your OS from the following location - http://code.google.com/p/chromedriver/downloads/list
To set up the firefox driver, the Firefox binary is assumed to be in the default location for your particular operating system such as for windows - %PROGRAMFILES%\Mozilla Firefox\firefox.exe (reference - http://code.google.com/p/selenium/wiki/FirefoxDriver)
If firefox is in it's default location you don't have to specify the firefox binary path. But if want to do that, check out this link - FirefoxBinary(java.io.File pathToFirefoxBinary) to make sure your constructors are correct.

Resources