Selenium automatically receives alerts

Does anyone know how to disable this? Or how to get text from alerts that were automatically accepted?

This code should 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

+4
source share
4 answers

The other day I answered something similar to this, so that it is still fresh. The reason your code fails is because if the warning does not appear at the time the code is processed, it will in most cases fail.

Fortunately , the guys at Selenium WebDriver are already waiting for this to complete. For your code, as simple as doing it:

 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 APIs from ExpectedConditions here , and if you want the code behind this method here .

This code also solves the problem because you cannot return alert.getText () after closing the warning, so I save it in a variable for you.

+5
source

Before you receive () a warning, you need to get the text. What you are doing right now is to accept (by clicking "OK") in the message , then , trying to get the text of the warnings after it leaves the screen, i.e. No warnings.

Try the following: I just added a line that retrieves the warning text, and then returns that line.

 driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); alert.accept(); return alertText; 
+1
source

Selenium webdriver does not wait to warn. Therefore, he will try to switch to a non-existent warning, and therefore he will fail.

For a quick and not-so-good fix, enter sleep .

The best solution would be to implement your own wait method for the alert before trying to switch to the alert.

UPDATE

Something like this, copy in here

 waitForAlert(WebDriver driver) { int i=0; while(i++<5) { try { Alert alert3 = driver.switchTo().alert(); break; } catch(NoAlertPresentException e) { Thread.sleep(1000) continue; } } } 
+1
source

The next method with the 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; } } 
+1
source

Source: https://habr.com/ru/post/1479807/


All Articles