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);
Related
I have been question about selenium test with chrome headless variant.
Sometimes test is waiting anything, why is it has long time 10 minutes?
I added into tests timeout 40 sec.
15:41:54 INFO: Selenium WebDriver v. 3.14.0 build time: 2018-08-02T20:19:58.91Z
15:41:54 Oct 12, 2018 12:41:55 PM com.codeborne.selenide.impl.WebDriverThreadLocalContainer createDriver
15:41:54 INFO: Create webdriver in current thread 1: ChromeDriver -> ChromeDriver: chrome on LINUX (6332d1a2dee8e95f05da4130b99237f9)
15:51:54 [34mEmbed Failed timeout
15:51:54 (Session info: chrome=69.0.3497.100)
15:51:54 (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 3.10.0-862.14.4.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
15:51:54 Command duration or timeout: 0 milliseconds
Selenium settings:
case "chrome":
testBrowserName = "CH";
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920,1080");
ChromeDriver driver = new ChromeDriver(chromeOptions);
WebDriverRunner.setWebDriver(driver);
break;
You havn't mentioned about your usecase in details and it is not clear why you would use restrict your tests to ChromeDriver implementation only.
If you use ChromeDriver driver = new ChromeDriver(); the ChromeDriver instance which will get created will be only able to invoke and act on the methods implemented by ChromeDriver and supported by Chrome Browser only. To act with other browsers you have to specifically create individual objects as below :
FirefoxDriver driver = new FirefoxDriver();
InternetExplorerDriver driver = new InternetExplorerDriver();
WebDriver Interface
From Selenium perspective, the WebDriver Interface is similar like a agreement which the 3rd party Browser Vendors like Mozilla, Chrome, Internet Explorer, Safari, etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available Browsers without any change.
You will find a detailed discussion in what is the difference between ChromeDriver and WebDriver in selenium?
Solution
Instead of using the ChromeDriver implementation switch to the WebDriver Interface. As per current scenario, we have to instantiate the implementations of WebDriver Interface directly. The current practice is, we need to write our Automated Test Script against this interface so that in future we may swap in a more fully featured Browser when there is a requirement for one.
Example:
case "chrome":
testBrowserName = "CH";
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920,1080");
WebDriver driver = new ChromeDriver(chromeOptions);
WebDriverRunner.setWebDriver(driver);
break;
I am developing something that want me to change the useragent.
In start I supply user agent as... (to chromedriver)
options.addArguments("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25");
But then I want to change it to Windows useragent, or just remove this useragent.
How can I achieve this programatically in Selenium Java.
No, you can't change the useragent of an active Browsing Session once you configure the WebDriver instance through an instance of ChromeOptions and addArguments attribute and start an active Browsing Session.
Even if you are able to extract the Session ID, Cookies, User Agent and other Session Attributes from the active Browsing Session still you won't be able to change/edit those attributes as a HOOK to the WebDriver.
To change the User Agent you have to re-configure the WebDriver instance and initiate a new Browsing Session.
As #Debanjan said You can't change user-agent in runtime, but did You try using ModifyHeader plugin, You can setup it to change user-agent but not at already instantiated driver. You can setup during webDriver instantiation and also try with ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/idgpnmonknjnojddfkpgkljpfnnfcklj.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
or
ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("user-agent=YOUR_USER_AGENT");
For IE you would use capabilities like this:
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
and possibly in combination with
driver.manage().deleteAllCookies();
How could this be achieved using Chrome and ChromeDriver?
While we work with Internet Explorer Driver we use the field IE_ENSURE_CLEAN_SESSION
IE_ENSURE_CLEAN_SESSION
As per the JavaDocs IE_ENSURE_CLEAN_SESSION is the Capability that defines whether to clean or not browser cache before launching Internet Explorer by IEDriverServer and is configured as follows :
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
Now let us have a look at GeckoDriver which follows the WebDriver Spec.
GeckoDriver / moz:profile / rust_mozprofile
If you have a closer look at the geckodriver logs closely you will observe that each time geckodriver is called a new moz:profile is scopped out and the details of rust_mozprofile occurs in the following line:
Marionette CONFIG Matched capabilities: {"browserName":"firefox","browserVersion":"56.0","platformName":"windows_nt","platformVersion":"6.2","pageLoadStrategy":"normal","acceptInsecureCerts":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"rotatable":false,"specificationLevel":0,"moz:processID":5848,"moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d","moz:accessibilityChecks":false,"moz:headless":false}
This log clearly indicates that Marionette scoops out a new "moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d" and this configuration is handled by the WebDriver instance i.e. the GeckoDriver.
You can find a more detailed discussion on moz:profile in Is it Firefox or Geckodriver, which creates “rust_mozprofile” directory discussion.
ChromeDriver
ChromeDriver which is following the same WebDriver Spec does abides (will be abiding) by the same suite.
Incase you are using any stored FirefoxProfile or ChromeProfile, WebDriver will pick up the existing profile where the Stored Browser Configurations are picked up for reuse.
driver.manage().deleteAllCookies();
Irespective of New/Existing FirefoxProfile or ChromeProfile if you add the line :
driver.manage().deleteAllCookies();
Only the cookies gets deleted only to be get restored back to support the Active Browser 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);
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.