I am using Java-Selenium to achieve automation of web pages and when I run test cases I get a popup in chrome browser to select a Certificate and I would like to know how can we detect if any Browser popup to select certificate is present so that I can do something about it.
I would like to get to know when any browser popup comes for selection of Certificate in Java-Selenium code.
try {
Alert alert = driver.switchTo().alert();
string message = alert.getText();
// accept alert
alert.accept();
}
catch (Exception ex)
{
}
Related
Currently in my tests when i select a button a pop up appears asking me to launch my web application
However I cant seem to switch onto this pop up as if its a window
new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allHandles = driver.getWindowHandles();
for(String winHandle:allHandles)
{
if (!first_handle.equalsIgnoreCase(winHandle))
{
driver.switchTo().window(winHandle);
}
}
And I also attempted to accept it as an alert, but it didnt recognise it as an alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
Ive seen suggestions to disable notifications and they dont work either, my main aim is to select the open button but I cant get any elements to select from the console either
You may need to disable notifications for a selenium web driver at a WebDriver level by adding options. Here is an example of how to do it for ChromeDriver:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "/home/users/user.user/Desktop/softwares/chromedriver");
WebDriver driver =new ChromeDriver(options);
driver.get("http://your.url/");
driver.manage().window().maximize();
I am not able to map the elements of the prompt because it is not possible to inspect them to get the ID or name of the login and password fields, I'm trying to code the code below, give a tab in the elements and even without success.
How do I automate login on this interface?
driver.get("url");
Alert alert = driver.switchTo().alert();
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyboardEvent.DOM_VK_TAB);
This seems to be a basic authentication popup. You can resolve it by passing the credentials in the URL itself as below:
driver.get("http://<username>:<password>#www.example.com");
Environment:
I have to test a web-application with Selenium. Before accessing the startpage, a windows security alert is thrown. Cause the alert/verification-popup is on os level, it can't be handled by the selenium alert-api.
Workaround:
To access the page, I wrote a script, which fills username and password and afterward clicks enter (script code from: https://automationtestingsimplified.wordpress.com/2011/08/11/how-to-handle-window-based-pop-up-using-selenium-and-autoit/).
Problem to solve:
Selenium throws "UnhandledAlertException: Modal dialog present", although the modal dialog is already accepted.
I tried:
Accessing Modal dialog via alert-api. Problem here is, I can not fill
in username. Even if I use Robot.
"driver.switchTo().alert().accept();" also doesn't work.
Let the driver wait till alert isn't present with
"wait.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()));"
To accept the dialog, event if it already has been accepted.
Possible solution:
Ignore dialog or set it as not present in the driver. But I can't find a solution for that.
Im Running into the same problem. until I find a better solution i'm using Robot to send the keys to the alert to accept it
private void acceptAlert() {
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
}
I am having a problem with certain websites that cause my browser to prompt an alert when I try to switch to a different URL, or even close the browser. Some examples:
http://grooveshark.com/
http://www.dollardays.com/
In order to workaround the alert with Selenium, I need to switch to that alert, and then sometimes accept it and sometimes reject it (depending on the contents of the alert).
I wish to avoid solving this problem that way because:
I need to guess whether I should accept the alert or reject the alert.
Switching to the alert sometimes throws an exception, even though the alert is present.
What preferences do I need to set in the Firefox-Profile, in order to prevent the browser from issuing such alerts (or any other alerts for that matter)?
Answers in Java or Python will be highly appreciated.
Thanks
To my knowledge you can only disable that behaviour globally.
There is a preference called dom.disable_beforeunload. You should change its value to true. With Selenium, you can create a new customized Firefox profile:
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("dom.disable_beforeunload", true);
FirefoxDriver driver = new FirefoxDriver(customProfile);
As far as I know it's not possible to disable native browser events like alerts, so you'll just have to handle them better.
1) You should be able to use alert.getText() to make an informed decision on whether to accept or dismiss an alert.
:
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
catch (Exception e) {
}
2) Use a WebDriverWait to avoid race conditions. See above
I don't think Firefox profile would feature disabling such specific elements, but you can hard-code some lines of static logic that would remain consistent across the test case/project.
Like Click on the main page automatically closes the pop-up frame/alert msg on grooveshark.com
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/#!/genre/Rap/1748"); //complete URL becomes http://grooveshark.com/#!/genre/Rap/1748
driver.findElement(By.linkText("moreā¦")).click(); // clicks a hyper-link which opens up that frame/pop-up
driver.findElement(By.id("lightbox-outer")).click(); // clicks outside the opened-up frame, or simply clicks on the main page in background
}
lightbox-outer is the main page.
You can't disable the popups (alert), just do alert.accept() means clicking the ok button of alert modal or alert.dismiss() means clicking the cancel or close button.
The worst in this case is that you need to wait a certain time if you are not very sure that alert is going to be present or not.
If the alert is present as a result of event sucess( just like clicing the button, webpage asks for your confirmation ) you dont need to do wait wait.until(ExpectedConditions.alertIsPresent());
you can save the time by going to next step which is switching the drver to alert. i.e
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
just to be sure you can use a small waiting time, but yes it depends uponthe network speed to load the web page for the case that alert is present when webpage is loaded.
When recording in selenium IDE I can click the "OK" button in a popup, and expected to be able to click it using
driver.findElement(By.linkText("OK")).click();
but this was not the case.
Similarly this doesn't work.
driver.switchTo().alert().accept();
Selenium throws a NoAlertPresent exception. If what's popping up is not an alert, then what is it? And how do I click yes!
in such case I'd prefer to check(verify) the alert presence on the page and then if is present - accept it.
It be somthing like:
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
}
here you can get details
Also do not forget about debug step by step.
Hope this helps you.
It could be anything. You should be telling us that.
If it is a Java Script alert then, this should work
driver.switchTo().alert().accept();
At the very least you could try sending enter/return key stroke, if the "OK" button is autoselected/highlighted by the web app.
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Update
It could also be because your alert is not present at the time you are trying to click/accept it.
For a quick check put in a sleep of 4-5 seconds and then try driver.switchTo().alert().accept();. Once it is ascertained, then put in a wait for alert present in a try and catch loop (any exception handling).
if you are using latest version of webdriver, infact anything above 2.20 then
driver.switchTo().alert().accept();
should work provided the alert is a javascript alert similar to the one we get when we click
alert demo OR confirm pop-up demo
Updated
here this code will help you accept the alert
driver = new FirefoxDriver();
String baseUrl = "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.switchTo().frame(0);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
driver.switchTo().alert().accept();