How to Click OK on chrome popup using Selenium Webdriver C# - c#

I'm trying to code below script for clicking OK button through script but fail to do it.
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
What am I doing wrong, Correct me.

I found an excellent nuget package, AutoItX.Dotnet, that will handle popups in chrome.
Link to nuget package - https://www.nuget.org/packages/AutoItX.Dotnet/
Please use the code below as a reference
//This code snippet will fix your specific issue
AutoItX.WinWait("Untitled - Google Chrome", "", 2);
AutoItX.WinActivate("Untitled - Google Chrome");
AutoItX.Send("{Enter}");
//Use code below to switch between buttons/text boxes within the popup
//And send text to text boxes within the popup
AutoItX.Send("{TAB}");
AutoItX.Send("HelloWorld");
You may have to test a bit further to see what keys you need to pass to make this code work seamlessly with each specific popup that you wish to interact with.

JavascriptExecutor should work for you. Just take care that you should execute it before clicking the event which invoke alert.
C# Code
IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.confirm = function(msg) { return true; }");
Java Code
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
Note :- do not use it after clicking on event which invoke alert confirmation box. Above code by default set the confirmation box as true means you are accepting/click on ok on all confirmation box on that page if invoked
Hope it will help you :)

Related

Selenium - Modal dialog present - how to accept information?

I have a following problem. After Submit some date on page I have a modal dialog like on the picture:
I want to click "ENTER" to go through that modal but it does not work.
I have following code:
driver.FindElement(By.CssSelector("input.submit")).Click();
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Enter);
After click on continue manually test go back to next page. I must go through this modal to continue the test. Any ideas how to solve this problem?
I found a solution by following code:
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
It works for me.
In java:
Alert alert = m_driver.switchTo().alert();
alert.accept();

Get alert content when alert is shown

I have an HTML page with this script:
<Script>
function Run()
{
alert("The Content Here");
return true;
}
</script>
<button onclick="Run();">I Want It Now</button>
Lets say that I opened this page with firefox or Chrome. And lets say that I clicked on the button "I Want It Now" and the page shows me the alert:
The Content Here
How can I insert the alert content into a string in my VB.NET project? I know that I can use the alert window handle to get the label handle and then extract (grab) the text of the alert, but I don't think that this is the best way to do it. Is there another way to pass (or get) information from the page (not from webbrowser control or by using webClient.DownloadString) into my VB.NET (or C#) project?
i don't this it will easy to use js without the webbrowser control by the way if you changed your mind and want to use a web browser control , you can do it like this :)
WebBrowser1.Document.GetElementById("NAME").InvokeMember("click");

How to click ok button in alert dialog box using watin?

I am trying to automate one website, in my project after clicking on 'submit' button, one alert message with ok button will come. I want to click that ok button.
I tried with these two codes seperatly but these are not working
AlertDialogHandler AlertDialog = new AlertDialogHandler();
ie.AddDialogHandler(AlertDialog);
ie.Button(Find.ByValue("Submit")).ClickNoWait();
AlertDialog.WaitUntilExists();
AlertDialog.OKButton.Click();
ie.WaitForComplete();
ie.RemoveDialogHandler(AlertDialog);
var AlertDialogHandler = new AlertDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, AlertDialogHandler))
{
ie.Button(Find.ByValue("Submit")).ClickNoWait();
AlertDialogHandler.WaitUntilExists(50);
var message = AlertDialogHandler.Message;
AlertDialogHandler.OKButton.Click();
ie.WaitForComplete();
}
While using these two codes, I got the same exception 'dialog not available within 30 seconds'.
Any help will be deeply appreciated. Thank You :)
This has happened with me a couple of times when I had multiple browser windows open and I was trying it out.
The solution at that time was to close all instances of IE, close NUnit / VS and start over again, and it worked like a charm. However, I was using a ConfirmDialogHandler and not an AlertDialog Handler.
If that does not help you might want to try adding the following before you fire up your browser instance.
Settings.AutoStartDialogWatcher = true;
Settings.AutoCloseDialogs = true;
I'm not familiar with WatiN, but here's another SO post that might help:
WatiN seems to not find JavaScript alert
There is a way to perform this without watin:
See my post:
Need a way to perform auto confirm for firefox or any other browser popup's
this is slightly unrelated but AlertDialogHandler's don't work for Firefox but there is a workaround...
http://pastebin.com/ZapXr9Yf

Selenium-RC Unable to handle Confirmation box

I am testing a web app where i delete an item from a list. Upon clicking on delete, the app asks for confirmation. Selenium IDE detects it as a confirmation box. When I run the code thro' the RC (C#), it even catches the confirmation-box, executes the click of delete button on that confirmation box, but, the confirmation box is never visible on the screen. Further, it only clicks on the delete button; the item doesn't get deleted. I tried it manually, works fine.
Please help, I'm new to Selenium and tried to find the answers at multiple forums without any success.
Here's the code:
string confirmation;
for (int second = 0;; second++) {
if (second >= 60) Assert.Fail("timeout");
try
{
confirmation=selenium.GetConfirmation();
if ((confirmation == " Delete confirmation message")) break;
}
catch (Exception e)
{
PrintLog("Error while waiting for confirmation. Error: "+e.Message);
}
Thread.Sleep(1000);
}
try
{
Assert.IsTrue(confirmation == "Delete confirmation message");
}
catch (AssertionException e)
{
PrintLog(e.Message);
}
selenium.FireEvent("//a[#id='btnOkConfirm']","click");
After the last statement, the selected item must get deleted and page should refresh, but, nothing happens. All I can see is "Javascript:;" written in the status bar of the firefox window. I guess its problematic to get javascript hrefs working in selenium-rc.
Thanks,
Vamyip
There are a number of commands to deal with JavaScript confirmations. Selenium will by default choose 'OK' at a confirmation, unless you send the chooseCancelOnNextConfirmation command. In order to consume the confirmation you will need to use the getConfirmation command.
Selenium reference for above commands:
http://release.seleniumhq.org/selenium-core/1.0/reference.html#storeConfirmation
http://release.seleniumhq.org/selenium-core/1.0/reference.html#chooseCancelOnNextConfirmation
http://release.seleniumhq.org/selenium-core/1.0/reference.html#chooseOkOnNextConfirmation
Additionally, if your click command is not showing the JavaScript confirmation you might find that the appropriate event is not being fired. You could try using the mouseDown and mouseUpcommands, or the fireEvent command.
Lately, I've discovered that this behavior is occurring due to the architecture of selenium(more precisely, its javascript based core). when I do the test manually, keeping the selenium IDE open, this behavior is replicated. So, I think there's no immediate solution to this problem right now. Will post here if I find a workaround.
Thanks to Dave for replying.
Update: The development team informed me that a javascript function is not being called with selenium IDE running alongside. This indeed is a problem with Selenium's Javascript core.
Thanks to everybody who took time to respond to this question.
Regards,
Vamyip

Open new window from code-behind

I need to open a new window from code-behind on post-back if a specific radio button is selected.
Is there any way to do this?
Thank you.
You can use RegisterStartupScript to send a window.open script to run once the page has loaded.
However, this will cause the majority of popup blockers to get in your way.
I think this should work ;-)
Add some javascript to your radio button to open a new blank window before you post back. This makes it so popup blockers won't block your popup, since it's opened in response to a users click. See this link for how to do this part.
Then, allow the postback to happen as normal and on page load, register a startup script to tell your already existing window to go to a new url.
String script = "window.open('popupPage.aspx', 'myPopup')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someId", script, true);
Note that in javascript, when you call
window.open(url, 'myPopup')
if a window already exists with that name it'll return it instead of creating a new window... So your popup won't get blocked!
You will need to run javascript on postback
Simple sample of RegisterStartupScript:
RegisterStartupScript("id1", "<script type=\"text/javascript\">alert(\"I'm from JavaScript.\");</script>");
New windows can be very sketchy, depending on the content that you need to present you might consider using an in window pop-in if you will. You will avoid pop-up blockers that way. If you can give more details we can give better answers.

Categories