This might be a repeated question but I could not find any solution. Recently I found a related post Connecting Selenium WebDriver to an existing browser session but people suggested me to ask a new question.
If any one have tried connecting selenium webdriver to existing browser session that was earlier spawned by selenium itself and had success in doing so, please let me know.
I could find couple of suggestions to try on firefox and selenium 2.X version. But those suggestions do not work for selenium 3.X and there are no solutions for chrome browser.
I have tried all suggestions for Selenium 25.3, firefox v 46 and it works. But for Chrome with chrome driver , I am not able to make it work.
Edited:
Here is the code I have tried:
Starting a firefox driver
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/StartFirefoxSession_lib/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
Copied RemoteWebDriver source code and changed capabilities from private to protected.
protected Capabilities capabilities;
Created a new class RemoteDriverEx extending the copied RemoteWebDriver class
Changed the NEW_SESSION command issued by the original driver to GET_CURRENT_URL
Response response = execute(DriverCommand.GET_CURRENT_URL, Collections.EMPTY_MAP);
Then craeted a JUnit test to verify
But I am struck with exception
org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'WPANDBW7HYD', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_74'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:154)
Full code shared # https://drive.google.com/open?id=0Bz2XxuQQc24KdHVqR3BPaXowUnM
It is possible in selenium all you need is a debugger address of the session you want to connect to. If you are wondering what is debugger address its nothing but the localhost address on which your session is running, it looks like localhost:60003. Now it will be different for each and every case. Below is process with c# code.
Get debugger Address of browser you want to connect later using debug mode as shown in snapshot below. debug driver after browser launch to fetch value
Now keep that browser running and to reconnect the same browser use below code.
ChromeOptions option = new ChromeOptions();
option.DebuggerAddress="localhost:60422";// we need to add this chrome option to connect the required session
driver = new ChromeDriver(option);
driver.Navigate().GoToUrl("https://www.google.com/");
Hope this helps!! let me know in comments if any clarification is required.
I managed to find a solution for Firefox in a hack way that works local:
First, you need to start a separate instance of browser (manual start) using the following arguments:
firefox.exe --marionette -profile C:\FirefoxTEMP
Above we open an instance of Firefox with --marionette turned on and we choose a fixed profile folder that were created just for selenium tasks.
Now, we will attach our automation to the already open Firefox window, by adding an argument to chose the same profile we started before.
Note: You must chose the same profile folder for the Webdriver use the open instance.
FirefoxOptions options = new FirefoxOptions();
options.AddArguments("--profile C:\\FirefoxTEMP");
driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("https://google.com");
Related
I'm trying to run some Selenium tests using the ChromeDriver, and I started getting this error:
Starting ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129) on port 42985
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"7576.1","isDefault":true},"id":1,"name":"","origin":"://"}
(Session info: chrome=54.0.2840.59)
(Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.48 seconds
Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700'
System info: host: 'SUJITH', ip: '192.168.43.228', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:683)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:143)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:170)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:159)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
at Selenium.Test2.main(Test2.java:15)
Why is this happening, and how can I fix it?
Update your chromedriver with lastest version. And please clarify your question.
https://chromedriver.storage.googleapis.com/index.html?path=2.24/
If your chrome browser is recently updated to V54.0.x, then you might get this error. The solution for this problem is simply to update your chrome driver.
Choose chrome driver from the below link that will suit your system architecture.
Latest Chromedriver v2.25
This issue started showing up after chrome Version 54.0.x updates.
Remove the older chromedriver from your path and update your chromedriver to 2.24. This should resolve your issue.
I had the same issue . Updating the chrome driver solved my problem.
A few things to bare in mind for these type of cases:
Disable browsers auto update - at least on your slaves to avoid these type of breaking your CI process. Link for how to on Chrome, and be sure to watch these settings on the installation.
Versioning is the first thing to check with Selenium when it breaks all of a sudden. See notes for version compatibility.
I'm using my own Maven plugin to check compatibility between chromedriver and Chrome version
Hope it helps...
You might be using the lower version of the chrome driver.
org.openqa.selenium.SessionNotCreatedException: session not created exception
I had the similar issue and was getting the exception as above. So always check the latest version of driver from here: https://sites.google.com/a/chromium.org/chromedriver/downloads
You might need to set in /etc/hosts
127.0.0.1 localhost
In my case the problem was that I kept a Selenium script running for too much time than I run out of disk free space.
So, I just clean out the Temp folder and created a routine to keep cleaning selenium folders, and problem solved.
You will get this error when your code looks like below:
WebDriver driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
You have to setProperty first and then create driver instance next
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
I am trying to upload file to Safari(8.0.8) using webdriver. Can anyone confirm it is possible or not? I was searching this problem and I cant find clear information.
My test environment:
I run test at my local PC with Win7 and browser start at Selenium Grid which is running on MAC machine (hub + node run at MAC Yosemite 10.10.5)
First, I try to upload direct file on MAC. But it is not working.
Browser.Driver.FindElement(By.Id("inputID")).SendKeys("/Users/administrator/Desktop/file.txt");
Next, I try to use LocalFileDetetor but it also doesn't work:
driver.FileDetector = new LocalFileDetector();
Browser.Driver.FindElement(By.Id("inputID")).SendKeys("c:\\file.txt");
Next, I try to use: WebDriverBackedSelenium:
ISelenium safari = new WebDriverBackedSelenium(webDriver, "http://systemname/");
safari.Start();
safari.AttachFile("xpath=//input[#id='inputID']", "e:\\file2.txt");
But it doesn't work too. Stack trace:
Selenium.SeleniumException : WebDriver exception thrown
----> OpenQA.Selenium.InvalidElementStateException : Element must be user-editable in order to clear it. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 7 milliseconds
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'mac.domain.company.com', ip: '192.168.136.67', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.safari.SafariDriver
Capabilities [{browserName=safari, takesScreenshot=true, javascriptEnabled=true, version=8.0.8, cssSelectorsEnabled=true, platform=MAC, secureSsl=true}]
Session ID: null
It doesn't work because it is Safari or there is problem with grid/safari/remote host or with file path(something with / )?
Looks like it is not supported https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/4220
You can upload using OSAScript. Please do the following:
Create .scpt file in mac with below code
activate application "Safari"
tell application "Safari"
tell document 1
do JavaScript "document.getElementsByTagName('label')[0].click()"
delay 2
end tell
end tell
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 2
keystroke "/Users/melamc/Downloads/upload.jpeg"
delay 2
keystroke return
delay 2
keystroke return
delay 2
end tell
trigger this file whenever you required (write a code to run this file programetically
I hope this will help you, try and let me know
I have done a lot of research to accomplish the uploading of file in safari browser on mac and fortunately I came up with the below solution.
Following are the possible prerequisites to match the given solution:
Programming Environment: c#
Automation Environment: Selenium WebDriver, Selenium Grid
Browser: Safari (12)
OS: MAC (High Sierra)
Hub: Windows machine, Node: Mac machine
In the context of the above to upload the file one can implement the following lines of code.
DesiredCapabilities dc = new DesiredCapabilities();
dc.SetCapability(CapabilityType.BrowserName, "safari");
dc.SetCapability(CapabilityType.Version, "12");
Driver = new RemoteWebDriver(new Uri("http://Node_Ip_Address:Port/wd/hub/"), dc);
Below lines of code do the magic, (This also works for Chrome, and Firefox.)
IAllowsFileDetection AllowsDetection = Driver as IAllowsFileDetection;
if (AllowsDetection != null)
{
AllowsDetection.FileDetector = new LocalFileDetector();
}
In java, the equivalent of the above is
Driver.setFileDetector(new LocalFileDetector());
Reference: https://saucelabs.com/blog/selenium-tips-uploading-files-in-remote-webdriver
The code line written for java has not been tested from my end as I am currently working in c#.
Happy Testing.
I am using selenium 2.47 with Firefox v31. With this simple implementation:
public void navigateToHomePage() throws Throwable {
System.out.println("Navigate to Home");
driver = new FirefoxDriver();
driver.quit();
}
I got this error:
org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
Build info: version: '2.47.0', revision: '0e4837e', time: '2015-07-29 22:49:49'
System info: host: 'ok-ThinkPad-SL500', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-24-generic', java.version: '1.7.0_79'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.internal.SocketLock.lock(SocketLock.java:99)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:90)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:276)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:116)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:223)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:216)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:212)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
at cucumber.features.StepDefinitions.navigateToHomePage(StepDefinitions.java:24)
at ✽.Given I navigate to the home site(/home/ok/workspace/CucumberPOC/src/cucumber/features/UserRegistry.feature:6)
I don't know what is it??
This SocketLock error happens when Firefox tries to bind to port 7054 and fails because another instance already has that port locked.
An immediate solution is make sure you have NO Firefox tasks or process running in the background before launching a new driver. Kill them all and try launching again.
In the long run, you can avoid these issues by creating a new profile, change the port preferences to avoid locks, and then launch the driver using that profile. A short example:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(FirefoxProfile.PORT_PREFERENCE, 7046)
driver = new FirefoxDriver(profile);
Unfortunately, when I was looking up the Java terms, I found that this feature was broken in Java's driver once, then it was fixed and then it broke in a later update, so even if you implement the solution I gave you, you may still get these errors in 2.47.
The fix was released in version 2.43.1, so if you don't need the newer versions for another issue/feature, you can try rolling back to a point where you can set a port preference successfully.
Not required reading: Some more technical details of the issue are in this pull request.
The piece of code is:
another edit: would like to note that I'm using java to implement this so I don't think slashes would be a problem. (though correct me if i'm wrong)
edit: One more thing i'd like to add is that it actually says its starting up the chrome driver version something but immediately fails after that
System.setProperty("webdriver.chrome.driver", "webdrivers/chromedriver.exe");
driver = new ChromeDriver();
and the error i'll end up getting is
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.
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: '****-PC', ip: '10.10.10.1', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_51'
There isn't a stack trace and this happens immediately after the webdriver attempts to start. I'm guessing the code above is where it happens simply because netbeans doens't really indicate where it errors out.
The mystery is that this worked on my computer but upon attempting to run it on a colleague's computer it simply produces this error. Firefox works for her but both IE and Chrome results in this. Any ideas?
edit: apparently there is a stack trace:
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:165)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
... 7 more
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:8891/status] to be available after 20002 ms
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104)
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163)
... 9 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79)
... 10 more
Caused by: java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(FutureTask.java:201)
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
... 11 more
You either need to add the absolute path which in Windows may need to include escaped backslashes e.g.
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
Or you can add the property to your System path.
If these options don't work it may be that there is a problem connecting to ChromeDriver. In which case you can open up the Chromedriver.exe file
WebDriver driver = new RemoteWebDriver("http://localhost:9515", DesiredCapabilities.chrome());
driver.get("http://www.google.com");
https://code.google.com/p/selenium/wiki/ChromeDriver
webdrivers/chromedriver.exe
I suspect it could not find chromedriver. Are you sure this is the right path to chromedriver.exe?
You should probably try with absolute path.
For example C:/Users/Name/Desktop/webdrivers/chromedriver.exe
Make sure that you are working with latest Chrome browser with one version behind and
The latest Chromedriver.exe version 2.9
The path should be mentioned like C://Test//chromedriver.exe
When I used the absolute path, it was giving me the error. However, when I used the directory where the executable was located, it started just fine. Here is a C# example
using OpenQA.Selenium.Chrome;
public class ChromeOptionsWithPrefs : ChromeOptions
{
public Dictionary<string, object> prefs { get; set; }
}
public static void Start()
{
var options = new ChromeOptionsWithPrefs();
options.AddArguement("-incognito");
using (IWebDriver driver = new ChromeDriver(#"C:\FilePath\", options))
{
//perform the test
driver.Navigate().GoToURL(#"http://www.google.com");
driver.Quit();
}
}
I just use the prefs to not store any data on whatever machine I am running the tests on. It is not needed, but you can pass some interesting options with it.
Unable to find/open Firefox Binary - webdriver/robot framework
My tests run fine in java and fitnesse. They also run fine when executing them through robot framework with Internet Explorer and Chrome. However when I execute them through Firefox, using 'new FirefoxDriver()', I receive the following error:
DEBUG java.lang.ExceptionInInitializerError
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java: 81)
Caused by: java.lang.NullPointerException
org.openqa.selenium.firefox.FirefoxBinary.<clinit>(FirefoxBinary.java: 42)
... 183 more
In the FirefoxBinary and FirefoxDriver classes these lines correspond to the following code:
FirefoxBinary ln42-43
private static final String PATH_PREFIX = "/" +
FirefoxBinary.class.getPackage().getName().replace(".", "/") + "/";
and FirefoxDriver ln 80-82
public FirefoxDriver(FirefoxProfile profile) {
this(new FirefoxBinary(), profile);
}
I have tried setting the path to the Firefox binary in my classpath, pythonpath (used by robotframework) and path. I have also written the following lines of code to try to force the binary to be found:
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(FirefoxDriver.BINARY, "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
I have tried to execute the tests on two computers, my work and home machines. Further I have tried to use a firefox profile created using firefox.exe –p and also by creating one in the java code. I have tried Firefox 6-8. Unfortunately none of these things have worked.
I am also using/have used:
Java 1.6
Selenium 2.9.0/2.13.0
Windows 7
I am unsure if this is related but as a work around I have been trying to get Firefox to run through a remote browser. I have been trying the following code:
rcc = new RemoteControlConfiguration();
rcc.setPort(4447);
rcc.setPortDriversShouldContact(4447);
rcc.setInteractive(true);
rcc.setSingleWindow(true);
rcc.setTimeoutInSeconds(30);
ss = new SeleniumServer(rcc);
ss.start();
DesiredCapabilities cap = new DesiredCapabilities();
cap.setJavascriptEnabled(true);
cap.setBrowserName("firefox");
URL url = new URL ("http://www.google.com/");
driver = new RemoteWebDriver(url,cap);
However when I run the above I get the following error message:
Exception in thread "main" org.openqa.selenium.WebDriverException: Error communicating with the remote browser. It may have died.
Build info: version: '2.13.0', revision: '14794', time: '2011-11-18 17:49:47'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0'
Driver info: driver.version: Selenium2Driver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:412)
Does anyone have any idea on how to fix either of my problems?
Any help would be greatly appreciated, I feel very stuck on this issue atm. Two days of trying to get Firefox to work when Internet Explorer already does….. It feels as if the world is about to end.
Thanks,
James
EDIT:1
It is possible for me to run Firefox by using selenium-server.
James, FYI, URL for RemoteWebDriver appears incorrect in post above. Should be something more like "localhost:4444/wd/hub";? Interestingly, I'm having the opposite problem with Web Driver, having issues starting Firefox via RemoteWebDriver but Firefox runs fine via native FirefoxDriver. IE works fine over remote. – David Dec 4 '11 at 4:51
Thanks David!
I am not understanding why you have not configured your Firefox binary in your remote grids config.json file? That's how I would do it. Then, your DesiredCapabilities object would not need to define it. A hint can be found here.
If it works, the line in the JSON file might look like:
"binary": "C:/Program Files/Mozilla Firefox/firefox.exe",
I guess it doesn't allow you to dynamically set the binary location from your code, but perhaps you can try it that way to prove if it should work or not as a troubleshooting step.
FirefoxProfile profile = new FirefoxProfile();
FirefoxBinary binary = new FirefoxBinary(new File("C:\\path to firefox\\firefox.exe"));
driver = new FirefoxDriver(binary, profile);
try this
This kind of issue obtained because of selenium web driver fail to find the .exe files of Firefox. Please check whether C:\Program Files (x86)\Mozilla Firefox you have exe file in the location and don’t forget to set environment variable having the java jdk path.
Source:- read [Solved Cannot find firefox binary in PATH Selenium][1]http://www.tech4crack.com/solved-cannot-find-firefox-binary-in-path/