How do I identify an existing open Chrome Window with a specific url in its address bar, and open a new tab in that window using Selenium web driver in C#? All examples I see shows how to open new tabs in a window that is opened within Selenium ChromeDriver.
IWebDriver driver = null;
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
driver = new ChromeDriver(chromeDriverService);
List<string> tabs = new List<string>(driver.WindowHandles);
driver.WindowHandles always return the windows opened by the ChromeDriver. But, I am looking for all windows. As soon as the line that instantiates ChromeDriver is executed, a new window opens up. But, I need a new URL to be opened in a new tab in an existing window.
WebDriver can't control browser windows that it didn't open. This is in part a security measure to prevent WebDriver-based malware. Additionally, to communicate with a browser instance, the browser must be listening on a port for incoming driver commands. Unless WebDriver started the browser, the browser has no way to know to listen on that port.
Related
Hello friends. I would like some help.
I've been trying to use the same instance of Firefox window open by Visual Studio at first time.
The question is, how to pickup the same window browser and continue to testing without it close the browser and open again every time i want to do a testing?
I know there is a lot of questions about this topic, but some that I've found is applicable for old versions of selenium and some codes didn't work due to be deprecated.
What i can do with success:
I can work normally with traditional method to test a unit, opening Firefox at beginning and close after the end of test:
[ClassInitialize]
public static void InitializeClass(TestContext testContext) {
driver = new FirefoxDriver();
}
Then i commented the following lines to keep the window open when test is complete:
//driver.Quit();
//driver.Close();
//driver.Dispose();
What i can't manage to:
With searches I've been made, i read that we can connect to an existing session already open before. So i tried with the following:
[ClassInitialize]
public static void InitializeClass(TestContext testContext) {
FirefoxOptions options = new FirefoxOptions();
driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);
//driver = new FirefoxDriver();
On the example above, I've commented driver = new FirefoxDriver(); and used the method FirefoxOptions instead of the deprecated DesiredCapabilities.firefox() and when i try to connect to the open firefox window, i got an error and i can't go on. On log, there's a line saying:
Was'n possible to copy the file "..\packages\Selenium.Firefox.WebDriver.0.27.0\build\driver\geckodriver.exe" to "..\bin\debug\geckodriver.exe"
The file geckodriver.exe (61484) is being used by another process
With this, i tried another different ways to reach what i expect like:
[ClassInitialize]
public static void InitializeClass(TestContext testContext) {
FirefoxOptions options = new FirefoxOptions();
driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);
options.AddArgument("--marionette-port " + "2828" + "--connect-existing");
FirefoxDriverService ffDriverService = FirefoxDriverService.CreateDefaultService();
ffDriverService.BrowserCommunicationPort = 4444;
}
for last i tried to open a separated instance of Firefox to try to connect to like below:
firefox.exe --marionette -start-debugger-server 2828 -profile %temp%\FirefoxTEMP
I got success on open a instance of marionette,but i continuing unable to attach the test to this open window.
I appreciate your attention and forgive me for some newbie techniques i made.
Questions I've read:
How to connect Selenium to existing Firefox browser? (Python)
How to connect Selenium Webdriver to existing Firefox/Chrome browser session?
How to connect to an already open browser?
edit:
take a look on this thread to understand that it's a common request:
Allow webdriver to attach to a running browser - GitHub
You can use "detach" and change Firefox to Chrome(because I didn't see that option in Firefox). Link to link. Shortly, it keeps your browser open depending on the option "true"/"false".
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("detach", true);
You can use bad habits and simply use a global variable.
global browser
I know, that such a question is already asked here before, but it is 4 years old and not quite helpful.
So, what i want to do is simple, i want my selenium (running in WPF and .Net 5.0 in c#) so connect to an open browser to control it. The browser is opened manually with the debuggingport 9222 and i can successfull connect to this browser from another browser instance.
i am able to open a NEW Chrome browser with selenium and control it, but i need to also have the ability to connect to an open instance.
and help would be awesome.
PS: i know that selenium is for testing, and normally not used to not be in a controlled test-environment. =)
thank you so much for you help guys.
Type : chrome://version/ in browser you will get executable path of chrome.exe
copy this and close all chrome instance.
**close all chrome instance and **
start chrome as:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=5555
verify chrome is started with port open by going to localhost:5555
Now add remote debugging port as:
var options = new ChromeOptions();
options.debuggerAddress= "127.0.0.1:5555";
var driver = new ChromeDriver(options);
use chrromeoptions to connect to that port from selenium
I have tried
options.add_argument("--headless")
driver = new ChromeDriver(options)
it's working fine but my requirement is to hide it after initialization.
As you can see here: How to execute tests with selenium webdriver while browser is minimized there is no way for the browser to be minified.
You could in theory move the browser out of view as described here https://sqa.stackexchange.com/questions/15484/how-to-minimize-the-browser-window-which-was-launched-via-selenium-webdriver
driver.manage().window().setPosition(new Point(-2000, 0))
Chrome headless is an instance of chrome. You can't start from normal mode and switch to headless mid test. Once you start testing with any kind of browser mode, you need to stick to it. There are some work arounds I've not tried with opening windows with code and continuing from the new window.
Use phantomJSDriver for Headless automation
WebDriver driver = new PhantomJSDriver();
I have a web API application. When I call some URL, selenium starts and goes to some site and makes screenshots and saves them to files.
When I run code in Visual Studio by pressing F5, the application works well.
But after I'd publish my application to IIS, I noticed that browser doesn't maximize and I got small screenshots.
I use a chrome driver, because IE driver and Firefox driver throw error
unable to connect.
I tried this code:
var chromeOptions = new ChromeOptions()
{
};
chromeOptions.AddArgument("--start-maximized");
using (IWebDriver driver = new ChromeDriver(chromeOptions))
{
driver.Manage().Window.Maximize();
/*driver.Manage().Window.Size = new System.Drawing.Size(1920, 1040); this doesn't work too*/
I notice when selenium starts under IIS I don't see a browser window. I only see a process of chrome in the task manager.
How to make maximize window of a browser under IIS?
Instead of :
chromeOptions.AddArgument("--start-maximized");
Can you try :
chromeOptions.AddArgument("start-maximized");
And remove :
driver.Manage().Window.Maximize();
I am using selenium WD in C# for cross browser testing but facing a strange problem that when ever i run my test using Nunit firstly Firefox window will open & then my desired browser window will open & run the test on it(desired browser).
As per my knowledge if any system is not having Firefox installed in it then it fails the script.
So is there any way to change this default value of browser in selenium.
I am able to run tests on different browser, my problem is only that before opening my desired browser by default first system is opening firefox. which create issue for me & my tests.
public void SetupTest()
{
driver = new SafariDriver();
baseURL = "http://google.com/";
verificationErrors = new StringBuilder();
}
Most probably, somewhere in your code you are initializing the Firefox Driver. Search for this within you code:
new FirefoxDriver();
You could also debug to the line
driver = new SafariDriver();
and see if it has an assigned value already.
But I am also pretty certain that you are initializing a FirefoxDriver somewhere.