Identify popup window Selenium in IE11 32 bit C# - c#

I am having trouble identifying a popup to handle it. This is what I tried:
string dialog4;
string dialog5;
try
{
IAlert alert = driver.SwitchTo().Alert();
dialog4 = alert.Text;
alert.Accept();
dialog5 = "nothing alert";
}
catch (NoAlertPresentException f)
{
try
{
dialog4 = driver.SwitchTo().Frame(0).Title;// "dialog4";//
dialog5 = driver.SwitchTo().Frame(1).Title;// "dialog5";//
}
catch (NoSuchFrameException e)
{
try
{
dialog4 = driver.SwitchTo().Window("iHTKK").Title;
dialog5 = "nothing window";
}
catch
{
dialog4 = "nothing 4";
dialog5 = "nothing 5";
}
}
}
The code wait 10 seconds and the webdrive wait 30 seconds after clicking the red-highlighted button to make sure that the pop-up has time to be caught.
And the code return "nothing 4" and "nothing 5".
xPath doesn't work very well in this project, I don't know why but I tried with different elements before but xPath do not work.
I could not open F12 unless I response to this pop-up. I could open it before and after the existence of this pop-up.
I also tried to find
in the website (all HTML and .js files) for the information in the pop-up box using search function in Developer tool (F12) in Debugger and Dom Explorer tabs.
Nothing found. This is not my website.
I attached the screen-shot with Window spy (an utility comes with AutoHotkey help identify windows). The pop-up was created by a process call "jp2lancher.exe". My Java version is 8.x 32 bit if that's of concern. While it seem like the pop-up is from a different process, I can't interact with IE until I response to that pop-up. However, I can close it via "Close all windows" action in the task bar and the pop-up will still exist.
What else can I do to identify this pop-up?

After testing around, it's confirmed that Window Spy tool is a good way to identify if a pop-up is generated by the browser/driver. In ahk_exe line, it will say a window is run by which executable file. In my case, the pop-up is indeed a Java plug-in that is loaded from the website and run on my computer. I finally use AutoIt to handle that window. If anyone use AutoIt, keep in mind that it is a DIFFERENT language and all actions must be initiated in its format.

Related

How to set the download folder for Edge in Selenium?

I am writing automated tests using Selenium. I want to set the download directory in Edge so that I can download files as part of my test. There is an EdgeOptions object that I can provide when creating the EdgeDriver, but I don't know what to set on the EdgeOptions.
I know the equivalent of how to do this in Chrome
chromeOptions.AddUserProfilePreference("download.default_directory", #"C:\temp")
and Firefox
firefoxOptions.SetPreference("browser.download.dir", #"C:\temp")
But, how do I do the same thing in Edge? And get it to download automatically without a save prompt?
As #Prany already mentioned, probably there is no way to set download automatically. And if I right understood, you want to handle with native window dialogue, when you are clicking on download button. Selenium cannot interact with native windows, but you can use this framework. The sample code would be like this:
// Press the A Key Down
KeyboardSimulator.KeyDown(Keys.A);
// Let the A Key back up
KeyboardSimulator.KeyUp(Keys.A);
// Press A down, and let up (same as two above)
KeyboardSimulator.KeyPress(Keys.A);
// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);
// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);
So you can simulate Ctrl + V keyboard action and Enter action. Hope it helps.
You can do this for Edge like this:
// hide driver Console? true/false
EdgeDriverService service = EdgeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true; // hide Console
// change Standard-Download-Path
EdgeOptions options = new EdgeOptions();
var downloadDirectory = "C:\temp";
// Setting custom download directory
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
// start Selenium Driver:
webdriver = new EdgeDriver(service, options);
// max. Window
webdriver.Manage().Window.Maximize();

Unable to handle authentication pop up

I am using C# , Selenium , AutoIt and Google Chrome.
I can launch the browser, and can see the authentication pop up.
Pop up window disappears when below code is executed and after that the browser stays there forever.
autoItX3 autoIt = new AutoItX3();
Driver.Instance.Manage().Window.Maximize();
Driver.Instance.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(2);
try
{
Driver.Instance.Navigate().GoToUrl(Driver.BaseAddress);
}
catch
{
return;
}
autoIt.WinWait("Authentication Required");
autoIt.WinActivate("Authentication Required");
autoIt.Send("admin");
autoIt.Send("{TAB}");
autoIt.Send("pass");
autoIt.Send("{ENTER}");
Driver.Instance.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(-1);
You are trying to automate a child window.
Autoit doesn't see child windows untill told to.
Opt("WinSearchChildren", 1) ;0=no, 1=search children also
Allows the window search routines to search child windows as well as
top-level windows. 0 = (default) Only search top-level windows 1 =
Search top-level and child windows
hard to make comment without knowing the internals of the authentication implementation on the server. One thing is sure - it is a bad idea from security view because parameters appended to the URL are not secure.
like : http://myURL.com/index.jsp/j_security_check?j_username=username&j_password=password
or
"http://username:password#www.example.com/")
this is what worked for me according to our internal authentication :
https://myURL.com/login/Login.aspx?usestandardlogin=1
so its => "http:YouURL.com" + "?" + "usestandardlogin=1"
now I am not seeing any pop up , it just re-direct me on login.

C# IE11 Automation - Cannot Connect To Open IE Window

I'm trying to connect to an Internet Explorer window that is already open. Once connected I need to send some keystrokes (via SendKeys) to the IE window for some processing. I've got the following code below that works up until the SendKeys command. It finds the IE window titled "Graphics Database". When it hits "SendKeys.Send("{TAB}");" I get the error "An unhandled exception of type 'System.NullReferenceException' occurred".
Additional information: I also get the following on the NullReferenceException error. The weird thing is if I code to open a new IE window and then use SendKeys it works fine. Connecting to an existing windows seems to cause this issue.
SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method.
Can anyone please help me figure out what to do to fix this?
Andy
InternetExplorer IE = null;
// Get all browser objects
ShellWindows allBrowsers = new ShellWindows();
if (allBrowsers.Count == 0)
{
throw new Exception("Cannot find IE");
}
// Attach to IE program process
foreach (InternetExplorer browser in allBrowsers)
{
if (browser.LocationName == "Graphics Database")
{
MessageBox.Show ("Found IE browser '" + browser.LocationName + "'");
IE = (InternetExplorer)browser;
}
}
IE.Visible = true;
System.Threading.Thread.Sleep(2000);
SendKeys.Send("{TAB}");
SendKeys.Send("G1007");
SendKeys.Send("{ENTER}");
I was able to resolve this issue. I could never get the IE.Visible = true to work. This seemed to do nothing in my code. I had to use the SetForegroundWindow() to set the focus to the IE window.
// Find the IE window
int hWnd = FindWindow(null, "Graphics Database - Internet Explorer");
if (hWnd > 0) // The IE window was found.
{
// Bring the IE window to the front.
SetForegroundWindow(hWnd);
This site helped me immensely with getting the SetForegroundWindow() working.
http://forums.codeguru.com/showthread.php?460402-C-General-How-do-I-activate-an-external-Window
Andy please bear with me as this will be long. First you are going to want to look mshtml documentation and Dom. https://msdn.microsoft.com/en-us/library/aa741314(v=vs.85).aspx I don't know why automation is so convoluted but it is. The UIautomation class works great for windows apps but has nothing really for IE that I've been able to find. Others will point to third parties like waitn and selenium. Waitn appears to no longer be supported and selenium won't let you grab an open IE browser. I have gone down this path recently because I wanted to be able to create an app to store my web passwords and auto fill them in since I can't save my username and passwords in browser due to security restrictions. I have an example here and hope it helps. First open up IE and navigate to http://aavtrain.com/index.asp. Then have a console project with mshtml referenced and shdocvw. Here is code below. It gets the window then finds elements for username, password, and submit. then populates the username and password and clicks the submit button. I don't have a login to this site so it won't log you in. I have been using it for my testing. Problem I have is sites with javascript login forms. If you get further with this info please post back as I am still trying to evolve the concepts and create something reusable.
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
Console.WriteLine("Starting Search\n\n\n");
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
if (ie.LocationURL.Contains("aavtrain"))
{
Console.WriteLine(ie.LocationURL);
Console.WriteLine("\n\n\n\n");
Console.WriteLine("FOUND!\n");
mshtml.HTMLDocument document = ie.Document;
mshtml.IHTMLElementCollection elCol = document.getElementsByName("user_name");
mshtml.IHTMLElementCollection elCol2 = document.getElementsByName("password");
mshtml.IHTMLElementCollection elCol3 = document.getElementsByName("Submit");
Console.WriteLine("AutofillPassword");
foreach (mshtml.IHTMLInputElement i in elCol)
{
i.defaultValue = "John";
}
foreach (mshtml.IHTMLInputElement i in elCol2)
{
i.defaultValue = "Password";
}
Console.WriteLine("Will Click Button in 2 seconds");
Thread.Sleep(2000);
foreach (mshtml.HTMLInputButtonElement i in elCol3)
{
i.click();
}
}
}
Console.WriteLine("Finished");

Selenium chrome driver click() method not always clicking on elements

I am writing integration tests in c# and when I use the click() method on certain elements inside a dialog box nothing happens and I get no errors. It will click some of the elements inside the dialog but not others. I thought if it wasn't selecting them properly then it would throw and exception but it runs smooth and says test passed even though it never actually clicked the button. The dialog box is an iframe.
I thought maybe it was trying to click a button that wasn't display yet or enabled so I added this before the click() call:
_driver.SwitchTo().Frame(_frameElement);
_wait.Until(d =>
{
var shippingInfoButton = d.FindElement(By.CssSelector("input[title ='Info']"));
return shippingInfoButton.Displayed && shippingInfoButton.Enabled;
});
var infoButton = _driver.FindElement(By.CssSelector("input[title ='Info']"));
ScrollToElement(infoButton);
infoButton.Click();
again this runs with no thrown exceptions so I'm assuming it has found the element and it is both displayed and enabled.
Let me know if you need any more info. Thanks
I can't explain why the selenium driver .click() method won't fire on some elements in the page but not others, but I did find a solution.
Using IJavaScriptExecutor you can click the element using javascript instead and in my case it worked.
Here is the code to run the IJavaScriptExecutor and below is my whole method.
//IJavaScriptExecutor
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", infoButton);
//my whole method for clicking the button and returning the page object
public ShippingMethodDetailsPageObject SelectShippingMethodInfo()
{
_driver.SwitchTo().Frame(_frameElement);
_wait.Until(d =>
{
var shippingInfoButton = d.FindElement(By.CssSelector("input[title='Info']"));
return shippingInfoButton.Displayed && shippingInfoButton.Enabled;
});
var infoButton = _driver.FindElement(By.CssSelector("input[title ='Info']"));
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", infoButton);
_driver.SwitchTo().DefaultContent();
return new ShippingMethodDetailsPageObject(_driver, false);
}
I ran into a similar problem. If it's the same problem there's a fault in the ChromeDriver it can't click certain elements because of surrounding divs etc. Bit lame really.
A simple fix is to send the Enter key e.g. element.SendKeys(Keys.Enter). Seems to work across all browsers.
I have some tests that works in Firefox all the times, and in Chrome it drove me mad, because sometimes it passed successfully, and sometimes the ".click" didn't work and it would fail the test.
Took a long time to notice it, but the reason was: I used to sometimes minimize the browser to 80% to be able to see the browser along side my IDE. it appears that the ".click" doesn't work when I did it.
At least for me this was the issue

watin attach to manually started window keeps failing

IList<string> values = new List<string>();
var instance = Find.By("hwnd", "110CC");
...
if(instance != null)
{
var ie = Browser.AttachTo<IE>(instance);
The browser instance is manually started by the tester in case this makes any difference.
This just doesn't work for me I keep getting an exception from watin saying that it can't find a window with that handle.
I got the handle with Spy++.
I tried searching by window title or window url also but it also didn't work.
Is there any way to do this?
Thank you
The below works as expected / no errors. WatiN 2.1, IE9, Win7
Before running the code, open an IE browser and point it at cnn.com
IE browser = Browser.AttachTo<IE>(Find.ByUrl("www.cnn.com"));
browser.TextField("hdr-search-box").TypeText("searchy");

Categories