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
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();
}
I am trying to write a Selenium test against Amazon site. I want to get "Sign in" element so that I can click on it.
url: www.amazon.es
Here is my Selenium Code:
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");
try
{
driver.findElement(By.id("nav-link-accountList")).click();
}
catch (Exception e)
{
System.out.println("Not Found");
}
Sometimes the code works correctly but sometimes it does not find the ID "nav-link-yourAccount". What is the problem? and how can I solve it?
Provide few seconds of wait, before click to this webelement so your driver may able to find the webelement.
For wait i am using Explicit wait method.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("nav-link-accountList"))));
driver.findElement(By.id("nav-link-accountList")).click();
The element which you are trying to click with id as nav-link-yourAccount is not clickable. To proceed further you need to click either the link with text Hola. Identifícate or the link with text Mi cuenta using one of the following xpaths:
//a[#id='nav-link-yourAccount']/span[text()='Hola. Identifícate']
or
//a[#id="nav-link-yourAccount"]/span[contains(text(),'Mi cuenta')]
Instead using implicit wait try using explicit wait for the login element.
I've tried with explicit wait over 50 click and it did works.
Here is code you can use.
public class dump {
public static void main(String a[]){
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 15);
for(int i=0; i<=50; i++){
driver.get("https://www.amazon.es");
try{
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='nav-link-accountList']")));
driver.findElement(By.xpath("//*[#id='nav-link-accountList']")).click();
System.out.println("clicked\t"+i);
}catch (Exception e){
e.printStackTrace();
System.out.println("Not Found");
}
}
}
}
Here is the proof of run:
All the best!!
Apply wait until element is appeared, so that it avoids NoSuchElementException and code is working without any error.
Below code is working fine:
driver.get("https://www.amazon.es");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement accontButton=driver.findElement(By.id("nav-link-accountList"));
WebDriverWait waitforelement=new WebDriverWait(driver,20);
waitforelement.until(ExpectedConditions.elementToBeClickable(accontButton));
try{
accontButton.click();
}
catch (Exception e){
System.out.println("Not Found");
}
Have you tried to find elements by xpath?
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");
try
{
driver.findElement(By.xpath("//*[#id='nav-link-accountList']")).click();
}catch (Exception e)
{
System.out.println("Not Found");
}
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
I'm new to Selenium & Java. What I'm trying to accomplish is to wait for an element to appear on a timeout, and if that element appears before the timeout runs down, then just keep running. The code below will give a TimeoutException and stop the rest of the code from running.
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
Please try this way:
public boolean isFind(WebElement my_element) {
try{
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
}
catch(TimeoutException exception) {
return false;
}
return true;
}
Thank you for all your help, it took me a bit of playing around with it. Here is the solution I came up with. Hope it helps the next guy. Let me know if I messed something up please, thanks again.
try{
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#id='signup_email']")));
}catch (org.openqa.selenium.TimeoutException e){
JOptionPane.showMessageDialog(null, "Login Complete!");
return;
}
You can use ExpectedConditions.visibilityOf
WebDriverWait wait = new WebDriverWait(d, 120);
wait.until(ExpectedConditions.visibilityOf(d.findElement(By.xpath("//input[#id='signup_email']"))));
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();