I have an automation scenario to perform a content searching. The problem happen in the search result screen where there are 2 Javascript alert popup appears at nearly the same time. I said nearly because the popup window appears one by one, not all of them at once. I managed to handle the first one, but not with the second one. NoAlertPresentException happened for the second alert handler code.
Here's where the alert happened:
public WSSPage enterAndSearchContent(String title) {
waitWSSPageLoaded();
waitForElementTextWithEnter(searchTextField, title);
waitWSSPageLoaded();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.dismiss();
wait.until(ExpectedConditions.alertIsPresent()); //NoAlertPresentException happened here
Alert alert2 = driver.switchTo().alert();
alert2.dismiss();
return PageFactory.initElements(driver, WSSPage.class);
}
I alse tried using alert.dismiss(); twice but still it gives NoAlertPresentException at the second alert.dismiss(); like the below code:
public WSSPage enterAndSearchContent(String title) {
waitWSSPageLoaded();
waitForElementTextWithEnter(searchTextField, title);
waitWSSPageLoaded();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.dismiss();
alert.dismiss(); //NoAlertPresentException happened here
return PageFactory.initElements(driver, WSSPage.class);
}
When I try manually dismissing the alert, the second alert appears almost instantly. I guess the problem is the code can't catch the second alert because it was too fast?
Is there any workaround for this problem? Thank you.
Note:
I'm using Firefox
Related
I am trying to Automate a web application , where I click on to next page and it throws a confirmation message which I could not handle it
I have used the below in my script
WebDriverWait wait = new WebDriverWait(Driver, 15);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
Error : Expected condition failed: waiting for alert to be present
So I think when you click on next button it open a new tab and displays
the confirmation JavaScript alert.
You can handle this for now only through Firefox geckoDriver this
is issue with the [ChromeDriver][1]
https://github.com/SeleniumHQ/selenium/issues/9040
I've tried like below, Just check it
WebDriver driver = new FirefoxDriver();
driver.navigate().
to("https://www.w3schools.com/jsref/event_onload.asp");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.xpath(".
(//a[contains(text(),'Try it Yourself ยป')])[1]"));
Set<String> originalWindowHandles =driver.getWindowHandles();
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
Set<String> updatedWindowHandles = driver.getWindowHandles();
for(String window: updatedWindowHandles)
{
if(!originalWindowHandles.contains(window)){
driver.switchTo().window(window);
break;
}
}
Thread.sleep(5000);
Alert alert = driver.switchTo().alert();
alert.accept();
}
How to put condition when sometimes there's popUp window but sometimes it does not appear.
try{
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert();
}
catch (NoAlertPresentException Ex)
{
System.out.println("No alert");
}
try the following
from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException
def example():
firefox_browser = webdriver.Firefox(executable_path=r'geckodriver.exe')
firefox_browser.get('https://www.google.com')
"""check if alert is displayed
"""
try:
alert = firefox_browser.switch_to.alert
except NoAlertPresentException:
print("No alert")
return False
else:
return True
if __name__ == "__main__":
example()
i am using WebDriverWait in Selenium, i want to wait 10 second to wait if some Alert or Confirm box will appear, if it appears i will accept it. for example if confirm box ask me to "Leave Page" or "Stay on Page" then i want to leave page automatically, and if alert is not appeared in 10 seconds then continue code Excecution instead of throwing Exception. or you can tell me some way to accept All confirm, alert boxes automatically during whole selenium test. thanks
here is my code
WebDriver driver = new FirefoxDriver();
driver.get("https:www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 10 );
if(wait.until(ExpectedConditions.alertIsPresent())==null)
{
System.out.println("alert was not present");}
else{
Alert alert = driver.switchTo().alert();
alert.accept();
System.out.println("alert was present");}
try/catch is useful to catch/handle the exception. Just place alert code in try block, if any exception occurs, it will handle in catch block and there after execution will continue..
WebDriver driver = new FirefoxDriver();
driver.get("https:www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 5 );
try{
if(wait.until(ExpectedConditions.alertIsPresent())==null)
{
System.out.println("alert was not present");
}else{
Alert alert = driver.switchTo().alert();
alert.accept();
System.out.println("alert was present");
}
}catch(Exception e){
System.out.println("ignored alret not present exception" +e.getMessage());
}
}
Thanks
i have a web page where i can create a document.
When i save the document i have a message like "Document successfully saved"
The message appears only for 3 seconds.
How can i find the element with webdriver to check the message has been displayed during my test.
In the html code, i tried to catch the code for this message, it appears for 3 seconds and the disappaeared.
The code is here:
<div class="alert alert-success top center alert-notify alert-icon>
Document successfully saved.
<span class="Close"></span>
I tried this without success:
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.accept();
} catch (Exception e) {
message = null;
}
System.out.println("message"+message);
return message;
}
then i tried this without success too:
driver.findElement(By.className("alert alert-success top center alert-notify")).isDisplayed();
Thread.sleep(3000);
System.out.println(" Alert message is displayed");
Can anyone help me please, i don't know how to handle with
your second try is the right way to detect the message, but your have to try for 3 seconds to find the element.
It should look like this
while (!elementFound || stillWithin3Seconds){
elementFound = findAlertByClassName()
}
assetTrue(elementFound)
You have to look all the time. In your code you are sleeping. When the dialog appears in this time, you won't notice it.
Does anyone know how to disable this? Or how to get the text from alerts that have been automatically accepted?
This code needs to work,
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
Alert alert = driver.switchTo().alert();
alert.accept();
return alert.getText();
but instead gives this error
No alert is present (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.14 seconds
I am using FF 20 with Selenium 2.32
Just the other day i've answered something similar to this so it's still fresh. The reason your code is failing is if the alert is not shown by the time the code is processed it will mostly fail.
Thankfully, the guys from Selenium WebDriver have a wait already implemented for it. For your code is as simple as doing this:
String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
// This will wait for a maximum of 5 seconds, everytime wait is used
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
wait.until(ExpectedConditions.alertIsPresent());
// Before you try to switch to the so given alert, he needs to be present.
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();
return alertText;
You can find all the API from ExpectedConditions here, and if you want the code behind this method here.
This code also solves the problem because you can't return alert.getText() after closing the alert, so i store in a variable for you.
Before you accept() the alert you need to get the text. What you're doing right now is accepting (clicking "OK") on the alert then trying to get the alerts text after it's out of the screen, i.e. no alert present.
Try the following, I just added a String that retrieves the alert text then return that string instead.
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
return alertText;
Selenium webdriver does not wait for the alert.
So it will try to switch to a non-existent alert and thats why it fails.
For a quick and not so good fix, put in a sleep.
A better solution would be to implement your own wait for alert method, before trying to switch to the alert.
UPDATE
Something like this, copy pasted from here
waitForAlert(WebDriver driver)
{
int i=0;
while(i++<5)
{
try
{
Alert alert3 = driver.switchTo().alert();
break;
}
catch(NoAlertPresentException e)
{
Thread.sleep(1000)
continue;
}
}
}
Following method with synchronized option will add more stability
protected Alert getAlert(long wait) throws InterruptedException
{
WebDriverWait waitTime = new WebDriverWait(driver, wait);
try
{
synchronized(waitTime)
{
Alert alert = driver.switchTo().alert();
// if present consume the alert
alert.accept();
return alert;
}
}
catch (NoAlertPresentException ex)
{
// Alert not present
return null;
}
}
Here is the JavaScript answer. The documentation has examples for all languages. https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/
await driver.wait(until.alertIsPresent());
el = driver.switchTo().alert();
await el.accept();