Windows Internet Explorer Modal dialog box preventing selenium test run - selenium

I am trying to execute test cases in IE 8. But sometimes I am getting Windows Internet Explorer Security Warning "Do you want to enable the security content for this page" or Modal dialog box "Stop running this script...". How do I handle it in selenium Webdriver. Any help will be greatly appreciated.

There is an option in InternetExplorerOptions which gives you the opportunity to automatically close the modal if unexpected.
This is how I handle in C# and this should be fairly similar in Java or other bindings as well.
var options = new InternetExplorerOptions { EnableNativeEvents = false };
options.EnsureCleanSession = true;
//This is the main hook
options.AddAdditionalCapability("disable-popup-blocking", true);
Driver = new InternetExplorerDriver(options);

Related

Selenium Webdriver and Embedded Browser in a Winform - How to connect web_driver instance?

I need to connect/create an instance of Selenium Webdriver to web content that my application under test opens in a Winform.
My test framework opens the Winform application with CodedUI, and further test steps cause the Winform application to open another Winform window that has a browser embedded in it. I need to create or tie a Selenium instance to this embedded browser content. It's using WebViewer/Edge in the embedded area.
Note - Not yet ready to move to WinAppDriver, if ever.
Resolved this. I found out that the web pane is an object called a WebView2.
Automating inside it requires the application to be launched in a special way:
Dim AppPath As String = "[full path to your app and its name].exe"
Dim edgeOptions As New EdgeOptions
edgeOptions.UseChromium = True
edgeOptions.UseWebView = True
edgeOptions.DebuggerAddress = "localhost:9222"
Dim AppStartInfo = New ProcessStartInfo(AppPath)
AppStartInfo.UseShellExecute = False
AppStartInfo.EnvironmentVariables("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS") = "--remote-debugging-port=9222"
Diagnostics.Process.Start(AppStartInfo)
You also need:
Selenium Support and Selenium Webdriver version 4.0.0-rc3 installed in the projects
msedgedriver from here: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
And finally in your automation code, when the window with WebView2 in it finally appears, you need to run this code to tie selenium webdriver to the WebView2 pane:
Dim driver As String = "[path to:]msedgedriver.exe"
Dim edgeOptions As New EdgeOptions
edgeOptions.UseChromium = True
edgeOptions.UseWebView = True
edgeOptions.DebuggerAddress = "localhost:9222"
Dim WebDriver = New EdgeDriver(EdgeDriverService.CreateDefaultService(".", driver), edgeOptions)

Using Selenium C# DevTools how to capture f12 options (Network) with c#

Capture F12 options using Selenium C# DevTools, need help on C# SELENIUM, Attached snapshot
Using Selenium C#, collect logs from F12 options on any website,
For example open google.com in chrome and press F12, right side you will see developer options, capture those network logs using selenium C#. Attached snapshot what to capture
Here is the code to access the Network tab. Below code block the css/jpg/png image on the website, you can use any Network method to collect logs or implement according to the requirement.
chromeDriver = new ChromeDriver();
IDevTools devTools = iConstants.chromeDriver as IDevTools;
IDevToolsSession session = devTools.GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
await domains.Network.Enable(new Network.EnableCommandSettings());
await domains.Network.SetBlockedURLs(new DevTools.Network.SetBlockedURLsCommandSettings()
{
Urls = new string[] { "*://*/*.css", "*://*/*.jpg", "*://*/*.png" }
});
chromeDriver.Manage().Window.Maximize();
chromeDriver.Url = "https://google.com";

How to inspect elements IE settings options -Selenium

I need to launch IE browser and click on IE settings option and do some action. I launched IE browser but not able to click on Settings as inspect element is only restricted to webelements but not to IE settings tools.
Does anyone know solution for this?
You can try pressing (ALt+X) command using any Robot or Action Class once you lauch your browser. It will open setting tab.
I try to search but did not get any way to access IE options and settings using Selenium.
If we talk from a browser side, Than browser will also not allow any automation to access settings.
Someone can miss use it and try to change browser settings without informing the user. Than it can be a security related issue.
Although, It is not possible to access browser setting panel.
But We can achieve some scenario using desired capability:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
//Examples
capabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
capabilities.setCapability("allow-blocked-content", true);
capabilities.setCapability("allowBlockedContent", true);
capabilities.setCapability("ignoreZoomSetting", true);
capabilities.setCapability("ignoreProtectedModeSettings", true);
capabilities.setCapability("requireWindowFocus", true);
capabilities.setCapability("enablePersistentHover", false);
capabilities.setBrowserName(DesiredCapabilities.internetExplorer().getBrowserName());
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
//it is used to initialize the IE driver
WebDriver driver = new InternetExplorerDriver(capabilities);
Note: Please provide exact details what you want to achieve.
As per your requirement >> add new entry to add site to apps option in tools
************ Solution ***********
Robot robot = new Robot();
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_X);
for (int i = 1; i <= 5; i++) {
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
}
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1000);
robot.keyRelease(KeyEvent.VK_ESCAPE);
Note: Above code will do, if not please handle according to your requirement.

Can we handle the chrome alert which appears while automating the webpage using selenium?

Please follow the below mentioned steps:
1.) go to this website : http://demo.guru99.com/V4/index.php
2.)punch in any junk login id and password , and then the dialog box appears says:
USER OR PASSWORD IS NOT VALID
3.)Q1: Is there any way I can punch in the "ok" button in this dialog box ???because I can't inspect the element of this dialog box.
4.)Q2: When u try to login to gmail [ password shouldn't be saved], after punching the login details, it asks, do you want to save this password for this site ? Can this popup be handled ? because here also I can't inspect any information .
Q1: Selenium can interact with javascript (browser based) alerts using the Alert interface.
You can try any of the following:
using the explicit wait
Wait<WebDriver> wait = new FluentWait<>(driver).withTimeout(5, TimeUnit.SECONDS).pollingEvery(500,msecs).ignoring(NoSuchElementException.class
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
directly getting the alert
driver.switchTo().alert().accept();
using webdriver / chromedriver capabilities (to handle unexpected alert exception)
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
Q2: I'm unable to replicate the save password popup for gmail. However, you should be able to do it by using .dismiss(); instead of .accept();.
Note: If you're using google chrome for testing, be reminded that chrome usually updates automatically. Selenium alert interaction may not work all of a sudden if it is not compatible with the latest chrome version. With this, I suggest you keep your chromedriver exe and java bindings updated.
Q1: Is there any way I can punch in the "ok" button in this dialog box ???because I can't inspect the element of this dialog box.
Answer: For scenarios where you need to handle an alert, you can write a generic method similar to the one mentioned below, and pass the appropriate arguments while invoking/calling it:
public static void handleAlert(WebDriver driver, boolean accept) {
Alert popup = driver.switchTo().alert();
if (accept)
popup.accept();
else
popup.dismiss();
}
While calling above method, you can pass true, when you want to accept the alert and false when you do not want to accept it.
Q2: When u try to login to gmail [ password shouldn't be saved], after punching the login details, it asks, do you want to save this password for this site ? Can this popup be handled ? because here also I can't inspect any information .
Answer: Solution in this scenario will depend on which browser is being used. As, you are using Chrome browser, you can use following to code to handle the required pop-up:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches", "--disable-extensions --disable-extensions-file-access-check --disable-extensions-http-throttling --disable-infobars --enable-automation --start-maximized");
options.setExperimentalOption("credentials_enable_service", false);
options.setExperimentalOption("profile.password_manager_enabled", false);
WebDriver driver = new ChromeDriver(options);
Let me know, if you have any further queries or above solutions do not work for you.
Try using JavascriptExecutor to accept alert:
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
Alert alert=driver.switchTo().alert();
alert.accept();
You can accept the alert box pop out as follows.
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();

Selenium throwing exception when a modal dialog is shown at start of ie instance

I have to automate a scenario where a modal dialog opens immediately after ie is open i.e.
from selenium import webdriver
driver=webdriver.Ie()
Once i do this, IE instance opens and an IE modal dialog opens on top of ie.
Now selenium closes this modal dialog immediately and throws an exception:
Unexpected alert present, Modal Dialog present.
But i have to handle this modal dialog.
Is there any way to handle this dialog and make sure selenium does not throw exception and quit?
Had the same problem, selenium dismissing the alert (as if Cancel was clicked), where you want to confirm (as if OK was clicked).
For me it worked to initialize the driver by initializing webdriver with the UnexpectedAlertBehavior option, as per below
private static InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions{ UnexpectedAlertBehavior = InternetExplorerUnexpectedAlertBehavior.Accept };
public static InternetExplorerDriver Driver = new InternetExplorerDriver(SeleniumLocator.GetSeleniumDriverPath(), internetExplorerOptions);