How can I disable provate browsing with selenium webdriver, and firefox browser? - java

This is my java code below. I use firefox profile settings. The problem is, I try to click a button, but the firefox tracking protection prevent the click. I tried to disable tracking protection, but if I turn off tracking protection mode, when the selenium driver open the browser, the browser forget my settings. I don't know why, and it's very annoying. How can I solve this problem?
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.stylesheet", 2);
profile.setPreference("permissions.default.image", 2);
System.setProperty("webdriver.gecko.driver", "F:\\path\\geckodriver.exe");
driver = new FirefoxDriver(profile);

You can use
FirefoxProfile profile = new FirefoxProfile();
profile .setPreference("browser.private.browsing.autostart", false)
profile.setPreference("permissions.default.stylesheet", 2);
profile.setPreference("permissions.default.image", 2);
System.setProperty("webdriver.gecko.driver", "F:\\path\\geckodriver.exe");
driver = new FirefoxDriver(profile);

Related

Authentication popup goes into the background Selenium, Firefox

I have had the problem for 2 weeks that when I create a new Firefox driver in Selenium, the authentication popup for the proxy is immediately pushed into the background. Selenium can't reach it there anymore. Do you have a solution to the problem? I am using Selenium 3.141.5, Java 1.8. and Firefox version 63.0.1.
System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver(options);
try {
Alert alert = driver.switchTo().alert();
alert.sendKeys("Username" + Keys.TAB + "Password");
alert.accept();
driver.switchTo().defaultContent();
}catch (NoAlertPresentException e) {
e.printStackTrace();
}
driver.get("https://www.google.de/");
EDIT: I tested it with Firefox version 62.0.3, everything works there.
Best way is to avoid the pop-up.
hit Win+R, run "firefox -p" and create new profile (let's call it selenium_profile)
run Firefox in selenium_profile, login to the proxy and save your credetials to Firefox
use the customized profile, there is my setup:
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", sec_var.driver_path);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
With custom browser profile you can use almost any settings modification, imported certificate (to avoid another auth pop-up), use extensions, ...
Basic auth pop-up you can avoid with send credentials in URL:
driver.get("https://username:password#www.example.com");
but it does not work in Chrome.
I have the exact same problem and have been researching everywhere for a workaround.
Here is what I've learned so far:
Using the previously saved custom browser profile that keeps your credentials saved (#pburgr suggestion) does the trick, however it also accumulates a lot more unintended and possibly undesired browser profile informations such as cookies, history and everything.
the auth pop up is not a normal pop up so it cannot be manipulated with selenium switch_to
In addition these type of credential pop ups cannot be inspected nor javascripted.
Another workaround is to use pyAutoGui to alt+tab and fill your credentials. Not a great solution though because you may have trouble into guessing how many alt+tabs until the pop up.
Bottomline: The most promising way would be loading a fresh clean browser profile, updating this profile by adding such credentials (I don't know how nor if feasible) and voilĂ , discarding this profile at the end of the session.

How to Handle Allow Location access alert of Firefox using selenium?

In Firefox i have set the location access to allow. but while automating the below popup is still shown. how to solve this?
I have used firefox options also.It's not working.
scenario: I have to select a city from the dropdown. Browser is showing alert
Allow location access to this site with two options .
1.Allow location,
2. Disallow
i have to click on "Allow location"
You can disable the notification popup as given below.
System.setProperty("webdriver.gecko.driver","./Resource/geckodriver.exe");
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("dom.webnotifications.enabled", false);
WebDriver driver = new FirefoxDriver(ffprofile);
driver.get("https://jabong.com");

Creating Firefox profile and switching off the marionette

I am coming from Ruby background, I know how to do this in Ruby Selenium Binding, but I don't know how to do it Java Selenium Binding,
I have this code to create Firefox profile
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
WebDriver driver=new FirefoxDriver(firefoxProfile);
It works in selenium 2.53 but it's throws error in very recent selenium binding 3.11.0, Can anyone tell me what's the alternative?
And also I wanted to switch off the marionette to connect to Legacy Firefox driver, I can do this with the following code
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", false);
WebDriver driver=new FirefoxDriver(capabilities);
But if I use the above line, then it gives the FirefoxDriver is deprecated. Can anyone guide me how to create profile as well as how to switch off the marionette?
Yes FirefoxDriver(desiredCapabilities) is deprecated.
Alternate way would be to go with options:
FirefoxOptions foptions = new FirefoxOptions(capabilities);
WebDriver driver=new FirefoxDriver(foptions);
Update : [In order]
FirefoxOptions foptions = new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
foptions.setProfile(firefoxProfile);
foptions.setCapability("marionette", false);
foptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe");
WebDriver driver = new FirefoxDriver(foptions);
To use an existing Firefox Profile for your Test Execution first you have to create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows. Now you have to pass the Firefox Profile to a FirefoxOptions class object. Additionally as you would be using the Legacy Firefox Browser
you have to set marionatte to false through a DesiredCapabilities class object which you need to merge() into the FirefoxOptions class object as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(testprofile);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
options.merge(dc);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
Update
I am not sure about your usecase and why you want to use Legacy Firefox Driver. But as per the GitHub discussion Unable to Start Firefox Using the Legacy Driver on a 3.5.3 Grid #jimevans clearly mentions :
The legacy Firefox driver won't work with Firefox 53 or so. You might get the browser to launch, but the language bindings will be entirely unable to communicate with the driver (because Firefox will refuse to load the browser extension that is the legacy Firefox driver).
#barancev also mentions :
A binding should not pass OSS capabilities in W3C-compliant parts of payload, in "capabilities" block. They are allowed in "desiredCapabilities" block only. Perhaps, Mozilla broke Selenium compatibility in Firefox 48 in release channel, but restored it in version 52 in esr channel. It was unexpected, but it's true.
It's all upto you to take a informed descission.

Starting chromedriver with saved passwords enabled

Everytime that I start the Chrome web browser, using chromedriver, it starts up clean with any custom settings disabled. I have a case where I am logged in on a website and I want to access the account to get some information. However, the newly opened browser is not logged into the account anymore. Even when I open a new browser manually I am still logged in on that same page. Is there a way to enable custom settings? Preferably in Java.
You can achieve this by using ChromeOptions as below :-
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeDirPath = "provided here a path where you want to custom chrome dir which could be use everty time you launch"
//ensure chromeDirPath exist in dir
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir="+chromeDirPath);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("url");
Now it will maintain your custom browser setting at chromeDirPath
Hope it will help you.

PDF is not being opened in Chrome started by Selenium

I am using Selenium and ChromeDriver 2.43.1 with the latest Chrome (Version 42.0.2311.135 at the time the quetion was asked). My web application generates a PDF. It is being sent with the correct MIME type and it also correctly opens in the Chrome PDF viewer. However when I try to open the PDF using Selenium in Chrome that is started by the WebDriver, it gets downloaded.
I believe it might be some settings that Selenium or WebDriver use to start Chrome.
I've tried settings a few switches, but nothing worked yet. My code:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("--please-make-it-work"); // not a real switch
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
webDriver = new ChromeDriver(capabilities);
webDriver.get(url);
What I really need is to start the browser in "normal" mode. It doesn't need any profile settings, just the defaults that will open the PDF.
The problem was caused by the recent change to the behaviour of the --test-type switch. It is described in the ChromeDriver issue tracker.
The workaround is to disable this switch. Here's my code changed:
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Arrays.asList("test-type"));
webDriver = new ChromeDriver(options);
webDriver.get(url);

Categories

Resources