I have one already opened IE browser , with some url.
After this, I Run below code which will open another IE browser. however it gives me only one window handle in below code.
Is it possible to get previously opened IE browser handle ?
IWebDriver IEdriver = new InternetExplorerDriver();
IReadOnlyCollection<String> browsers = IEdriver.WindowHandles;
foreach (String item in browsers)
{
IEdriver.SwitchTo().Window(item);
String url = IEdriver.Url;
}
I think this is what you're looking for:
String winHandleBefore = driver.getWindowHandle();
//Do whatever operations you have to do
for(String winHandle : IEdriver.getWindowHandles()){
IEdriver.switchTo().window(winHandle);
}
Be careful as what you are trying to do, is not robust solution to write tests cases. If one test case causes browser to crash you will be getting all the test cases failed.
Also I don't think it should be possible to get handles on previously opened window by default, because when you write code
IWebDriver IEdriver = new InternetExplorerDriver();
It calls for a constructor of InternetExplorerDriver class and opens new instance of Internet Explorer.
You can either go for close all browsers before starting your test case execution by killing the ie process from task manager.
foreach (Process process in Process.GetProcessesByName("iexplore"))
{
process.Kill();
}
Related
I use Selenium ChromeDriver to open chrome browser and load a site into it programmatically.
I install selenium & ChromeDrive from this NuGet
Install-Package Selenium.WebDriver -Version 3.141.0 Install-Package
Selenium.WebDriver.ChromeDriver -Version 77.0.3865.4000
I have some questions:
if target pc has no chrome browser installed then how can i capture it by ChromeDriver ? is it possible?
when i am opening chrome browser by ChromeDriver instance then browser is opening chrome browser with a site but another CUI window is getting opened which i do not want to visible or i want to hide this CUI window. if it is not possible then how could i open this CUI window in minimize state?
a sample CUI window screen shot attached below when i work with FirefoxDriver. the same occur when i work with ChromeDriver instance.
when i executing this code chromeDriver.Close(); then opened chrome browser is getting closed but CUI window is still open. so if i click 5 times on open button then 5 CUI window is getting open along with 5 chrome browser instance which i had to close manually ....which i do not want to manually close it rather i want to close it when browser will be closed....how to achieve it ?
how to capture from code that opened chrome browser is close by this code chromeDriver.Close(); or if user click on cross button of chrome browser to close it?
how to open a new tab in already opened chrome browser instead of opening new chrome browser instance. if no chrome browser is open at all then new chrome browser will be open...how to achieve it by code. this below code opening new chrome browser always....what to change there for my point 5
chromeDriver = new FirefoxDriver(options);
chromeDriver.Navigate().GoToUrl("https://www.google.com");
another issue occur when i work with chrome driver that. it open chrome browser but a notification appear on browser like Chrome is being controlled by automated test software
I search google to hide it and found people said to use this option
options.setExperimentalOption("excludeSwitches", new String[] { "enable-automation" });
at my end this function does not available setExperimentalOption so what to do?
Please answer point wise with sample code.
For question 2,3 you can use below code
(use DriverService.Dispose(); to manually dispose driver service) :
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace MyProject
{
public class Browser : IDisposable
{
bool disposed = false;
IWebDriver Driver;
public Browser()
{
//Chrome Driver copied on startup path
ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(Application.StartupPath, "chromedriver.exe");
//hide driver service command prompt window
driverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
//hide browser if you need
//options.AddArgument("headless");
//or this to hiding browser
//options.AddArgument("--window-position=-32000,-32000");
//On offer Dona bhatt for disable automated test notification
options.AddExcludedArgument("enable-automation");
//options.AddArgument("disable-infobars");
Driver = new ChromeDriver(driverService, options);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
Driver.Close();
Driver.Quit();
Driver.Dispose();
DriverService.Dispose();
}
disposed = true;
}
//this method for navigation
public string Navigate(string url)
{
string page = string.Empty;
try
{
Driver.Navigate().GoToUrl(url);
page =Driver.PageSource;
}
catch
{
}
return page;
}
//this method for wait to an element be visible by element ID
private void WaitUntilLoad(string id, int timeOut)
{
WebDriverWait waitForElement = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut));
try
{
waitForElement.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
}
catch (WebDriverTimeoutException e)
{
}
}
}
}
Use this class:
using(Browser brw=new Browser())
{
string pageSource=brw.Navigate("My URL");
}
I couldn't post this as a comment because it's too long. I think this question is off topic but I just noted my comments on your items...
This seems to be much to broad. Are you having a problem with any specific issue? All of these seems like things you can research and find out.
1) The chrome driver has to be downloaded and shipped with your app.
2) It might be possible to hide that window but not sure why that would be a hard requirement. I'm going to just say that you can't by default prevent the window from showing.
3) You're going to have to close the chrome windows through your .net code. Selenium isn't going to be graceful enough to terminate all the chrome windows.
4) I'm not sure what you're asking for. What's the problem?
5) I think tabs might be done very differently between browsers, it might not be something natively supported in selenium. There might be some chrome driver commands that can facilitate but I have no idea what they are.
6) Again, this is typically not the preferred experience, I'm guessing one of your packages has a different featureset than whatever you're reading was using.
I am attempting to control two browser windows via selenium using c# and a single chromedriver. The reason being that I need to share session details accross browser windows.
The code that I have tried and failed with is below;
var options = new ChromeOptions();
options.AddArguments("chrome.switches", "--disable-extensions --disable-extensions-file-access-check --disable-extensions-http-throttling --disable-infobars --enable-automation ");
options.AddUserProfilePreference("credentials_enable_service", false);
options.AddUserProfilePreference("profile.password_manager_enabled", false);
options.PageLoadStrategy = PageLoadStrategy.Default;
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
var Driver = new ChromeDriver(service, options);
//THIS WILL OPEN A NEW WINDOW. BUT BECAUSE IT IS A NEW DRIVER DOES NOT WORK FOR SHARING SESSION DETAILS.
//var TestDriver = new ChromeDriver(service, options);
//TestDriver.Manage().Window.Maximize();
//THIS JUST OPENS UP A NEW TAB. NOT A NEW WINDOW (IT WOULD SEEM MOST DOCUMENTATION SUGGESTS THAT IT SHOULD)
IJavaScriptExecutor jscript = Driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open();", "google.com.au");
//TRY USING THE SEND KEYS TECHNIQUE. NOTHING HAPPENS
var test = Driver.FindElement(By.TagName("html"));
test.SendKeys(Keys.Control + "n");
test.SendKeys(Keys.Control + "t");
//TRY AGAIN USING THE SEND KEYS TECHNIQUE USING A DIFFERENT TAG. NOTHING HAPPENS
var blah = Driver.FindElements(By.TagName("body"));
blah[0].SendKeys(Keys.Control + "t");
//TRY USING ACTIONS. NOTHING HAPPENS
Actions action = new Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "n");
action.Build().Perform();
I may resort to AutoIt to open a browser if I have to, but one more dependency is not what I need. Documentation everywhere around the web seems to suggest than all the options I tried above should work...I suspect it may be a chromedriver issue of some kind.
Any ideas on how to achieve my goal would be greatly appreciated
UPDATE.
Arnons answer below lead me to the solution. If you are in a similar situation the best thing to do is just open up the browser console (from developers tools) and experiment with javascript until you get what you want. Then just execute that. In the end executing the following code has worked for me.
IJavaScriptExecutor jscript = Driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open('https://www.bing.com.au','_blank','toolbar = 0, location = 0, menubar = 0')");
The other alternative was to use Autoit, which I also got working, much easier than I did figuring out the javascript. But one less dependency is best :)
UPDATE2.
Further complications arise with trying to control the window as an independent browser window. I believe any new window created from a parent window, has the same process id (at least my testing has indicated so), and for all intense and purpose is treated as a tab in the selinium driver. I therefore conclude that certain things are just not possible (for example relocating the child browser window on the screen).
Your first attempt using ExecuteJavaScript was very close, but In order for it to open a new window instead of new tab, you should add the following arguments: `"_blank", "toolbar=0,location=0,menubar=0" to it.
See this question for more details.
I should have read the question better, here is my solution. Ended up using this for selecting windows that popped up after clicking a button but should work with swapping between windows.
//---- Setup Handles ----
//Create a Handle to come back to window 1
string currentHandle = driver.CurrentWindowHandle;
//Creates a target handle for window 2
string popupWindowHandle = wait.Until<string>((d) =>
{
string foundHandle = null;
// Subtract out the list of known handles. In the case of a single
// popup, the newHandles list will only have one value.
List<string> newHandles = driver.WindowHandles.Except(originalHandles).ToList();
if (newHandles.Count > 0)
{
foundHandle = newHandles[0];
}
return foundHandle;
});
//Now you can use these next 2 lines to continuously swap
//Swaps to window 2
driver.SwitchTo().Window(popupWindowHandle);
// Do stuff here in second window
//Swap back to window 1
driver.SwitchTo().Window(currentHandle);
// Do stuff here in first window
You need to explicitly tell Selenium which tab you wish to interact with, which in this case would be;
driver.SwitchTo().Window(driver.WindowHandles.Last());
I have an application that monitors a file, and based on its contents launches a browser using this code:
Process.Start("iexplore", "-nomerge " + fullUrl);
Now as with many small projects, the requirements have changed. The change is that only one browser can be launched from my program at a time.
Also, another program is capable of launching a browser with another url, and I cannot shut it down, eliminating the option of just closing down all instances of iexplore and then launching mine. ( which is what I did originally )
Is there a way to launch a browser and continue to keep control of it so you can update the URL of that specific instance of iexplore using c#?
This code is close to what is in the answer on the duplicate post, but is slightly different, so I am sharing it here.
foreach (SHDocVw.InternetExplorer ie in new SHDocVw.ShellWindowsClass())
{
if (ie.FullName.ToLower().Contains("iexplore") &
ie.LocationURL.ToLower().Contains("&qtype=mine"))
{
ie.Visible = true;
ie.Navigate(fullUrl);
openNewBrowserWindowWindow = false;
}
}
if (openNewBrowserWindowWindow) {
SHDocVw.InternetExplorerClass IE = new SHDocVw.InternetExplorerClass();
IE.Visible = true;
IE.Navigate(fullUrl);
}
In visual studio 2010, working with c#;
I open a browser with:
private IE browser;
private void Set_Browser()
{
string splashUrl = "google.com";
browser= new IE(splashUrl);
}
If a user(person) closes the browser by accident, then my application will not be able to work anymore.
QUESTIONS:
So how do I check if a user closed the browser manually?
Can I unable a user from closing the browser by adding the browser to my
application GUI as a control? [using Windows Forms]
-> How do I do that?
Last question related to this post How to use watin with WebBrowser control? (2 years old, but no decent answer too)
EDIT: The solution in give URL seems to work. Problem is that if I try to send the WebBrowser.ActivateX.. as an object to other class. Then my browser = new IE(..) returns null. It does work when I instantiate it in the form class though. Any solutions?
You can search for the process of internet explorer every x seconds and see if the browser is already running using this code:
bool isRunning = false;
foreach (Process clsProcess in Process.GetProcesses()) {
if (clsProcess.ProcessName.Contains("iexplore"))
{
isRunning = true;
break;
}
}
You can use this article
Or you can add a browser control to your application using this article
One thing you can do is hide the browser to avoid users closing it .. See this SO question.
Hiding Internet Explorer when WatiN is run
Is there a way I can launch a tab (not a new Window) in Google Chrome with a specific URL loaded into it from a custom app? My application is coded in C# (.NET 4 Full).
I'm performing some actions via SOAP from C# and once successfully completed, I want the user to be presented with the end results via the browser.
This whole setup is for our internal network and not for public consumption - hence, I can afford to target a specific browser only. I am targetting Chrome only, for various reasons.
As a simplification to chrfin's response, since Chrome should be on the run path if installed, you could just call:
Process.Start("chrome.exe", "http://www.YourUrl.com");
This seem to work as expected for me, opening a new tab if Chrome is already open.
// open in default browser
Process.Start("http://www.stackoverflow.net");
// open in Internet Explorer
Process.Start("iexplore", #"http://www.stackoverflow.net/");
// open in Firefox
Process.Start("firefox", #"http://www.stackoverflow.net/");
// open in Google Chrome
Process.Start("chrome", #"http://www.stackoverflow.net/");
For .Net core 3.0 I had to use
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = #"http://www.stackoverflow.net/";
process.Start();
UPDATE: Please see Dylan's or d.c's anwer for a little easier (and more stable) solution, which does not rely on Chrome beeing installed in LocalAppData!
Even if I agree with Daniel Hilgarth to open a new tab in chrome you just need to execute chrome.exe with your URL as the argument:
Process.Start(#"%AppData%\..\Local\Google\Chrome\Application\chrome.exe",
"http:\\www.YourUrl.com");
If the user doesn't have Chrome, it will throw an exception like this:
//chrome.exe http://xxx.xxx.xxx --incognito
//chrome.exe http://xxx.xxx.xxx -incognito
//chrome.exe --incognito http://xxx.xxx.xxx
//chrome.exe -incognito http://xxx.xxx.xxx
private static void Chrome(string link)
{
string url = "";
if (!string.IsNullOrEmpty(link)) //if empty just run the browser
{
if (link.Contains('.')) //check if it's an url or a google search
{
url = link;
}
else
{
url = "https://www.google.com/search?q=" + link.Replace(" ", "+");
}
}
try
{
Process.Start("chrome.exe", url + " --incognito");
}
catch (System.ComponentModel.Win32Exception e)
{
MessageBox.Show("Unable to find Google Chrome...",
"chrome.exe not found!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}