How to use/attach an existing browser using Selenium? - selenium-webdriver

What is the Selenium equivalent to attaching an existing browser in Watir?
brw = Watir::IE.attach(:title, "Google")

This is a duplicate answer
**Reconnect to a driver in python selenium **
This is applicable on all drivers.
1. open a driver
driver = webdriver.Firefox()
2. extract to session_id and _url from driver object.
url = driver.command_executor._url #"http://127.0.0.1:60622/hub"
session_id = driver.session_id #'4e167f26-dc1d-4f51-a207-f761eaf73c31'
3. Use these two parameter to connect to your driver.
driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.session_id = session_id
And you are connected to your driver again.
driver.get("http://www.mrsmart.in")

Run a Webdriver first
driver = new FirefoxDriver();
Now run a RemoteWebdriver
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://localhost:7055/hub"),capabilities);
The RemoteWebdriver will attach to the first browser window running Webdriver and it will not create a new window.
Note: Run Webdriver (1) and RemoteWebdriver (2) in separate programs one-by-one).

Related

How to attach a Selenium ChromeDriver to an embedded CefSharp browser in a WPF application?

I have a ChromiumWebBrowser control in my MainWindow.xaml:
<cefSharp:ChromiumWebBrowser x:Name="chromiumBrowser" Address="https://www.github.com/"/>
and I'm trying to drive it by attaching a Selenium ChromeDriver to it:
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = AppDomain.CurrentDomain.FriendlyName;
options.AddArguments("disable-extensions");
options.AddArguments("disable-plugins");
ChromeDriver chromeDriver = new ChromeDriver(options);
chromeDriver.Url = "http://www.reddit.com";
Is this a correct approach? It seems to nearly work as the embedded control flashes once when the driver attempts to connect to it but then I get the following error from the ChromeDriver console:
[1006/220430.356:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
[1006/220430.357:ERROR:cache_util.cc(139)] Unable to move cache folder GPUCache to old_GPUCache_000
EDIT
Apparently attaching a driver using options.BinaryLocation will unfortunately always launch a new instance of the executable. From the comments below, I gathered that the correct way would be to attach the driver through a debug port. So I create my browser control through code:
CefSettings settings = new CefSettings();
settings.RemoteDebuggingPort = 9222;
settings.UserDataPath = "C:/Temp";
Cef.Initialize(settings);
browserControl = new ChromiumWebBrowser("chrome://version");
But when I try to attach to it:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--remote-debugging-port=9222");
ChromeDriver driver = new ChromeDriver(options);
driver.Url = "http://www.reddit.com";
The error is now:
System.InvalidOperationException: 'session not created from disconnected: unable to connect to renderer (Session info: chrome=85.0.4183.121) (SessionNotCreated)'
This could be due to mismatching chrome versions as CefSharp only supports up to v84 at the moment. I'm using a v84 chromedriver so I don't know where this 85 warning comes from.
After sustaining some massive hair loss, I found out that this works:
CefSettings settings = new CefSettings();
settings.RemoteDebuggingPort = 9222;
settings.UserDataPath = "C:/Temp";
Cef.Initialize(settings);
browserControl = new ChromiumWebBrowser("chrome://version");
And this is how to gloriously attach to the embedded ChromiumWebBrowser:
ChromeOptions options = new ChromeOptions();
options.DebuggerAddress = "localhost:9222";
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
ChromeDriver driver = new ChromeDriver(service, options);
The trick was to use ChromeOptions.DebuggerAddress instead of ChromeOptions.AddArguments("--remote-debugging-port=XXXX")

Authentication pop up chrome browser

How to handle authentication pop up in chrome browser by using selenium web driver?
Try:
WebDriver driver = new ChromeDriver(options)
driver.get("http://username:password#www.domain.com");
or
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to(testURL);
References:
https://sites.google.com/a/chromium.org/chromedriver/capabilities
https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md

Remote WebDriver UnreachableBrowserException: Could not start a new session

I got this exception for all browsers. For example, I create a remote webdriver on chrome like this:
caps = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
caps.setCapability(ChromeOptions.CAPABILITY, options);
webDriver = new RemoteWebDriver(new URL("http://myIP:5555/wd/hub"), caps);
And I got UnreachableBrowserException as follow:
org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
But I check my selenium hub at http://myIP:4444/grid/console, everything is fine, the node is stil registered. I then check my node at http://myIP:5555/wd/hub/static/resource/hub.html, I still can click "Create Session" to create a session for all browsers.
I just got this exception today, it still worked few days ago. I am using Selenium 3.11.0, IntelliJ 2017.3, all drivers and browsers are latest versions.
I googled here, but I can't find a solution for this while my gird is still running. Any help much appreciated.
The error says it all :
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
The current implementation of Selenium while invoking RemoteWebDriver supports the ChromeOptions and you can use the following code block :
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
webDriver = new RemoteWebDriver(new URL("http://myIP:5555/wd/hub"), options);
Update
As per your comment update the documentation at seleniumhq-documentation is yet to be updated. Here are the relevant bytes from the Selenium Release Notes :
Selenium v3.5.0 :
* Start making *Option classes instances of Capabilities. This allows
the user to do:
`WebDriver driver = new RemoteWebDriver(new InternetExplorerOptions());`
Selenium v3.6.0 :
* All `*Option` classes now extend `MutableCapbilities`
`new RemoteWebDriver(new ChromeOptions());`
Selenium v3.7.0 :
* Migrated from using `DesiredCapabilities` to either
`MutableCapabilities` or (preferably) `ImmutableCapabilities`.

Selenium Webdriver + Java - How to configure automatic proxy configuration URL for firefox?

I'm trying to configure my FirefoxDriver to use automatic proxy configuration URL. I am failing to do so.
My code looks like this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
profile.setPreference("network.proxy.autoconfig_url", "http://10.203.193.52/Proxy-cmv.pac");
WebDriver driver = new FirefoxDriver();
I am not receiving any kind of errors but the connection is not working for this browser. When checking Options > Advanced > Network > Connection Settings from Firefox menu, proxy is set to "Use system proxy".
You need to use that profile, because now you just create it.
So, change:
WebDriver driver = new FirefoxDriver();
to
WebDriver driver = new FirefoxDriver(profile);

Issue while handling proxy prompts with the Firefox driver

I am facing issue with the proxy prompts for firefox browser with version 17.
I tried the steps mentioned in this link. But still am getting the prompts.
Also referred selenium document for using proxies, but unable to succeed.
I need to use FF17 only as per my project req and am using selenium 2.28.
My company proxy addr is proxy.comp_name.com port: 8080.
I am not getting any proxy prompts while doing manually.
While running multiple tests prompt will be displayed arbitrarily.
Any updates on the below issue?
code used to launch the driver with the default firefox profile
FirefoxProfile profile = new ProfilesIni().getProfile("default");
DesiredCapabilities dCap = DesiredCapabilities.firefox();
dCap.setCapability(FirefoxDriver.PROFILE, profile);
driver = new FirefoxDriver(dCap);
code used to launch the driver with the new profile:
String PROXY = "proxy.abc.com:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabailities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
Also I tried by setting Preferences to the firefox profile, but still am getting the proxy prompts..
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("network.proxy.type", ProxyType.SYSTEM.ordinal());
firefoxProfile.setPreference("signon.autologin.proxy" , true );
firefoxProfile.setEnableNativeEvents(false);
desiredCapabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
return new FirefoxDriver(desiredCapabilities);
Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("");
// We use firefox as an example here.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// You could use any webdriver implementation here
WebDriver driver = new FirefoxDriver(capabilities);

Resources