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.
Related
Is it possible to use proxy after starting chromedriver on the same browser?
EX:
I start chrome driver
Load website
Put in info
Use proxy
Click submit
I think i found some ways to do it in python and JS but im not sure how to convert it to java
As per Selenium's current implementation once you configure the WebDriver instance with the required Options and Capabilities and initialize the WebDriver session to open a Web Browser, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.
So, in-order to use a proxy you have to initiate a new WebDriver session.
here is #JimEvans clear and concise comment (as of Oct 24 '13 at 13:02) related to proxy settings capability:
When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.
You can find a relevant discussion in Set capability on already running selenium webdriver
You can use ChromeOptions class.
You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions
object into the ChromeDriver constructor:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);
ChromeOptions options = new ChromeOptions();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
options.setCapability("proxy", proxy);
// Add a ChromeDriver-specific capability.
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);
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.
I have requirement that everytime i login, it asking security code in webapp. But it will ask only once since browser stored the cookies.but it is asking again and again in selenium webdriver since driver always opening new browser every time.
So i need to use already opened browser in selenium webdriver java. please help me with example code.
Finally i found solution that using google chrome option in java. It stores cookies in your temp file and reusing it everytime.
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir=D:/temp/");
capabilities.setCapability("chrome.binary","res/chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
driver1 = new ChromeDriver(capabilities);
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);
I have cookies with Gmail login info, so that chrome automatically opens my Gmail.
I tried the following code, but it didn't work:
System.setProperty("webdriver.chrome.driver","chromedriver\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default"));
//I also tried using: capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir = C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default"));
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://gmail.com");
I checked the directory of C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default it is okay.
What is the problem in here?
There is a Known issues section on the Chrome Driver official wiki page I haven't noticed before:
Known Issues
3 . Cannot specify a custom profile
Now, I don't know whether this is or isn't outdated. I could not find a bug report for this. It's true that you can't specify a custom profile via Capabilities (as of July 2013), as you discovered. But there is a solution...
The Solution
Here's how I managed to make it run:
ChromeOptions opt = new ChromeOptions();
opt.setBinary("E:\\some\\path\\chrome.exe");
opt.addArguments("--user-data-dir=C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(opt);
Notice the path to the User data directory - it does not have the \\Default part. And in that case, it works just fine for me, opens up the Chrome profile stored with all the cookies and logins.
I have no idea why the Capabilities solution does not work. It might be worthwile to file a bug as I could not find one on topic.