selenium
Pop-upmeldingen accepteren met Selenium
Zoeken…
Python-voorbeeld van Accepting alert
from selenium import webdriver
# Create a new webdriver
driver = webdriver.Chrome()
# Get a page that has a popup window (Use mouse to click "try it" button)
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert")
# Accept the opened alert
driver.switch_to.alert.accept()
C # Extensies naar WebDriver
public static IWebDriver dismissAlert(this IWebDriver driver)
{
try
{
IAlert alert = driver.SwitchTo().Alert();
alert.Dismiss();
}
catch {}
return driver;
}
public static IWebDriver acceptAlert(this IWebDriver driver)
{
try
{
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
}
catch { }
return driver;
}
Hoe te gebruiken:
driver.acceptAlert();
driver.dismissAlert();
Java
Voor eenvoudige melding:
Alert simpleAlert = driver.switchTo().alert();
String alertText = simpleAlert.getText();
System.out.println("Alert text is " + alertText);
simpleAlert.accept();
Voor promptmelding:
Alert promptAlert = driver.switchTo().alert();
String alertText = promptAlert.getText();
System.out.println("Alert text is " + alertText);
//Send some text to the alert
promptAlert.sendKeys("Accepting the alert");
Thread.sleep(4000); //This sleep is not necessary, just for demonstration
promptAlert.accept();
Voor bevestiging alert:
Alert confirmationAlert = driver.switchTo().alert();
String alertText = confirmationAlert.getText();
System.out.println("Alert text is " + alertText);
confirmationAlert.accept();
Een andere manier om dit te doen, is je code in een try-catch te wikkelen:
try{
// Your logic here.
} catch(UnhandledAlertException e){
Alert alert = driver.switchTo().alert();
alert.accept();
}
// Continue.
Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow