I am working on a C# project where I am trying to send a excel file by WhatsApp. The problem is that whenever I run the code a new instance of command window is open, and a new window is opened for chrome every time I ran the code. Before running the code, I have already log-in to my WhatsApp account and wants to use the same window and tab to avoid logging each time. I have tried to use several ways to stop this but failed to achieve the result. Please correct the code. Please don't mind my bad English.
var options = new ChromeOptions();
options.AddArguments(#"user-data-dir=C:\Users\Umesh Aggarwal\AppData\Local\Google\Chrome\User Data");
IWebDriver driver = new ChromeDriver(#"C:\Users\Umesh Aggarwal\Desktop\chromedriver_win32", options);
////// Navigate to Whatsapp web
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
IReadOnlyCollection<string> windowHandles = driver.WindowHandles;
// Find already opened window with Chrome
string chromeWindow = "";
foreach (string window in windowHandles)
{
driver.SwitchTo().Window(window);
if (driver.Title.Contains("Google Chrome"))
{
chromeWindow = window;
break;
}
}
// Switch to Chrome window
driver.SwitchTo().Window(chromeWindow);
// Get all open tabs in Chrome window
IReadOnlyCollection<string> tabHandles = driver.WindowHandles;
// Find already opened tab of Whatsapp web
string whatsappTab = "";
foreach (string tab in tabHandles)
{
driver.SwitchTo().Window(tab);
if (driver.Title.Contains("Whatsapp"))
{
whatsappTab = tab;
break;
}
}
//// Switch to Whatsapp tab
driver.SwitchTo().Window(whatsappTab);
Related
The problem is that I need to get the PID of IE browser instances so that I can close the IE browser(Working in C#).
I launched the IE browser using Selenium and then used Driver Service class as :-
InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService();
Console.WriteLine(driverdetails.Port);
The plan is to get the port and then have its child process. I am able to do so using a debugger by entering the value of Port manually. But, the port fetched by driverdetails.Port was not the actual port used by my driver.
Is there any was, I can find the Port for any given driver service?
For IE i have an alternative to launch IE and get the URL with port which says http://localhost:. However, this is not the case with other browsers. My want is to make the generic code and hence I am using the Driver Service object.
As far as I know, the InternetExplorerDriverService's ProcessID property gets the process ID of the running driver service executable, and we can't get the IE browser instance PID through the InternetExplorer webdriver. If you want to get the PID, you could try to use the Process class.
From your description, it seems that you want to close the IE tab or window by using the IE Webdriver. If that is the case, I suggest you could use InternetExplorerDriver WindowHandles to get the opened windows, then use the switchto method to switch the window and check the url or title, finally, call the Close method to close the IE window. Please refer to the following sample code:
private const string URL = #"https://dillion132.github.io/login.html";
private const string IE_DRIVER_PATH = #"D:\Downloads\webdriver\IEDriverServer_x64_3.14.0"; // where the Selenium IE webdriver EXE is.
static void Main(string[] args)
{
InternetExplorerOptions opts2 = new InternetExplorerOptions() { InitialBrowserUrl = "https://www.bing.com", IntroduceInstabilityByIgnoringProtectedModeSettings = true, IgnoreZoomLevel = true };
using (var driver = new InternetExplorerDriver(IE_DRIVER_PATH, opts2))
{
driver.Navigate();
Thread.Sleep(5000);
//execute javascript script
var element = driver.FindElementById("sb_form_q");
var script = "document.getElementById('sb_form_q').value = 'webdriver'; console.log('webdriver')";
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript(script, element);
InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService(IE_DRIVER_PATH);
Console.WriteLine(driverdetails.Port);
// open multiple IE windows using webdriver.
string url = "https://www.google.com/";
string javaScript = "window.open('" + url + "','_blank');";
IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)driver;
jsExecutor.ExecuteScript(javaScript);
Thread.Sleep(5000);
//get all opened windows (by using IE Webdriver )
var windowlist = driver.WindowHandles;
Console.WriteLine(windowlist.Count);
//loop through the list and switchto the window, and then check the url
if(windowlist.Count > 1)
{
foreach (var item in windowlist)
{
driver.SwitchTo().Window(item);
Console.WriteLine(driver.Url);
if(driver.Url.Contains("https://www.bing.com"))
{
driver.Close(); //use the Close method to close the window. The Quit method will close the browser window and dispose the webdriver.
}
}
}
Console.ReadKey();
}
Console.ReadKey();
}
This My Code
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
ChromeOptions cOptions = new ChromeOptions();
String pathToExtension = #"C:\Users\Evrenus\AppData\Local\Google\Chrome\User Data\Default\Extensions\cfhdojbkjhnklbpkdaibdccddilifddb\3.4.1_0";
cOptions.AddArgument("load-extension=" + pathToExtension);
cOptions.AddArgument("--window-size=2000,1190");
cdriver = new ChromeDriver(service, cOptions);
cdriver.Navigate().GoToUrl(link);
int sayi = 0;
foreach (var item in cdriver.WindowHandles)
{
if (sayi == 1)
{
cdriver.SwitchTo().Window(item).Close();
}
++sayi;
}
When i was trigger this code i see chrome my screen.
This application is twitch chat bot and while i streaming (usually game), viewers can open songs from my computer but my computer ALT + TAB
i try this code on my Form Load Event
Selenium Chrome window running in second monitor screen stealing focus
I have written several Selenium Website Tests, however, this is my first with a site that is set to Windows Authentication.
I have been trying with Chrome and IE.
With IE I can successfully login to the site, and I see AutoIt typing and I can see that the browser successfully loads the correct page after login, however, the driver never has a connection to that browser.
With Chrome, I can never find the window to connect with AutoIt.
Using C#. See inline code comments for more specifics on the errors.
try
{
var autoIT = new AutoItX3();
if (IE) //IE Driver - Driver looses connection with the window immediately on Navigate, but AutoIT login works.
//(Driver does work fine if I go to a non-windows authentication site)
{
driver.Navigate().GoToUrl(testConfig.Url);
System.Threading.Thread.Sleep(2000);
//driver.CurrentWindowHandle crashes, 'No Window Exits'
var driverHandles = driver.WindowHandles; //result is 0
var test4 = autoIT.WinActive("Windows Security");
autoIT.WinActivate("Windows Security");
autoIT.Send("zzzzzzz");//Don't need user as it remembers
System.Threading.Thread.Sleep(1000);
autoIT.Send("{TAB}");
autoIT.Send("{ENTER}");
//Visually I can see Browser is logged in and full page loads.
autoIT.WinActivate("EDB Data Manager - Internet Explorer");
var test5 = autoIT.WinGetTitle("EDB Data Manager - Internet Explorer");
System.Threading.Thread.Sleep(2000);
driver.Url = testConfig.Url;
System.Threading.Thread.Sleep(2000);
var driverHandles2 = driver.WindowHandles; //result is still 0
//this will fail
IWebElement userName = driver.FindElement(By.Id("WellsCard"));
//I've tried refreshing or navigating to the url again with no success.
}
else //chrome Driver Driver maintains connection with the window immediately, but AutoIT fails, as I can't find window.
{
driver.Navigate().GoToUrl(testConfig.Url);
System.Threading.Thread.Sleep(2000);
var test7 = autoIT.WinExists("Authentication Required");
var tes8 = autoIT.WinExists("data:, - Google Chrome");
var test9 = autoIT.WinExists("EDB Data Manager - Internet Explorer");
var test10 = autoIT.WinExists("Untitled - Google Chrome");
//None exist, so this will not work.
autoIT.WinActivate("Authentication Required");
autoIT.Send("myUserName");
autoIT.Send("{TAB}");
autoIT.Send("zzzzz");
System.Threading.Thread.Sleep(1000);
autoIT.Send("{TAB}");
autoIT.Send("{ENTER}");
}
}
catch (Exception ex)
{
output.OutputMessage(ex.ToString());
}
I want to be able to change the directory of an existing explorer window. Is there an api call to send a "navigate there" message to a window (perhaps with a handle to it)?
First, add a reference to the Microsoft Internet Control library. Then you can use the following code, assuming you already know the window handle for your explorer window:
var shellWindows = new SHDocVw.ShellWindows();
var myFolder = "C:\\temp"; // folder name you want to navigate to
var myHwnd = 0; // whatever window handle you're looking for
foreach (SHDocVw.InternetExplorer shellWindow in shellWindows)
{
if (shellWindow.HWND == myHwnd)
{
shellWindow.Navigate(myFolder);
break;
}
}
When IE opens a second browser window using onclick.window.open() I need to do some processing on the second window and then close the window.
I have tried the following recomendations found on stackoverflow regarding window handles:
string popupHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = selObj.Driver.WindowHandles;
foreach (string handle in windowHandles)
{
if (handle != originalHandle)
{
popupHandle = handle;
break;
}
}
selObj.Driver.SwitchTo().Window((popupHandle));
selObj.Driver.Close();
selObj.Driver.SwitchTo().Window(originalHandle);
Before I execute this code I attempt to click on a control of the new window to change focus.
Webdriver cannot locate the control (I also tried to use the WebDriverWait class to wait for the second browse to fully load.
WebDriverWait also throws an exception after the wait seconds.
Upon attempting to execute the:
selObj.Driver.SwitchTo().Window((popupHandle));
I receive the following exception:
No response from server for url http://
I know this is C# and IE, wondering if anyone has found a work around for this?
Thanks
My trio C#+WebDriver+Ie works fine.
Yes - IE has to be focused.
Please, Try my code. Code works with IE, Chrome and Firefox
protected void SwitchToWindow(string name)
{
foreach (string item in _driver.WindowHandles)
{
if (_driver.SwitchTo().Window(item).Title.Contains(name))
{
_driver.SwitchTo().Window(item);
break;
}
}
}
Where _driver is selObj.Driver for your case.
A little bit late but can be effective :
var ieOptions = new InternetExplorerOptions
{
IntroduceInstabilityByIgnoringProtectedModeSettings = true
};
ieOptions.ForceCreateProcessApi = true;
ieOptions.BrowserCommandLineArguments = "-private";
driver = new InternetExplorerDriver(IE_DRIVER_PATH, ieOptions);
You have to set ForceCreateProcessApi at true. It's only with IE that you have to set this kind of options.
And if the program threw an other error like :
"This issue occurs when the following registry entry is set to 0:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\TabProcGrowth"
You will have to create a key in register (using regedit) and set this key
to 0 (Type: REG_DWORD and Value Type: Decimal).