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();
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 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)
{
}
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.
I am getting error message as "No alert is active" when i run the script using Java in Webdriver. This is inconsistent behavior, it gives error sometimes and it does not give error. When I didn't this error message, it will fail at some other location.
Note: The script is working perfectly in my machine.
I have changed all the IE 9 settings in the other machine with my machine setting, from then i am getting this error. previous, i use to get the error message as "Modal Dialog Present".
Please find the code i have used below:
//Entering data in the Account Information section
driver.findElement(By.name(OR.getProperty("AccName_Id"))).clear();
driver.findElement(By.name(OR.getProperty("AccName_Id"))).
sendKeys(AccName+"_"+Randnum);
driver.findElement(By.name(OR.getProperty("AccDBA_ID"))).sendKeys("Kumar_DBA");
driver.findElement(By.id(OR.getProperty("AccTax_ID"))).sendKeys(""+numberToSend);
driver.findElement(By.name(OR.getProperty("AccAddress_ID"))).sendKeys("124 - City
Cross Roads");
driver.findElement(By.id(OR.getProperty("AccZip_ID"))).sendKeys("00501");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccZip_ID"))),
"").perform();
//Entering data in the Main Contact Information section
driver.findElement(By.name(OR.getProperty("AccFName_ID"))).sendKeys("Kumara");
driver.findElement(By.name(OR.getProperty("AccMName_ID"))).sendKeys("S");
driver.findElement(By.name(OR.getProperty("AccLName_ID"))).sendKeys("Swamy");
driver.findElement(By.id(OR.getProperty("AccOffNo_ID"))).sendKeys("1234567890");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccOffNo_ID"))),
"").perform();
driver.findElement(By.id(OR.getProperty("AccCellNo_ID"))).sendKeys("0123456789");
new
Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccCellNo_ID"))),
"").perform();
driver.findElement(By.id(OR.getProperty("AccFaxNo_ID"))).sendKeys("1234567890");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccFaxNo_ID"))),
"").perform();
driver.findElement(By.name(OR.getProperty("AccEmail_ID"))).sendKeys("abc#abc.com") ;
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
driver.switchTo().alert().accept();
isAlertPresent();
Thread.sleep(15000);
driver.findElement(By.id(OR.getProperty("AccZip_ID"))).sendKeys("79081");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccZip_ID"))),
"").perform();
Thread.sleep(8000L);
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
driver.switchTo().alert().accept();
isAlertPresent();
Thread.sleep(30000);
Please help me on this issue ASAP. Help will be appreciated deeply.
I think your web driver code fired before the alert renders. My suggest is to put wait for alert after click action.
Try this code:
//Do some click action to render alert.
//statement to wait for an alert.
WebDriverWait wait = new WebDriverWait(driver,
30);
wait.until(ExpectedConditions.alertIsPresent());
//accepting the alert
driver.switchTo().alert().accept();
The above code will wait for 30 seconds and check the alert presence every 500ms. If the alert is present then it will terminate the wait and do the next action.