好的,所以我知道在这个问题上有很多关于webdriver警报的其他答案,我已经看过了但是我觉得我的情况有点不同。当我单击我的提交按钮时,我已经切换到3帧,然后我收到警报,所以我尝试切换回默认内容,然后使用try catch和alert.accept单击警报,但它仍然没有单击警报。代码如下。在此先感谢您的帮助:)
public class BookAHoliday {
public FirstPage completeHolidayFormAndSubmit(String firstDate, String lastDate) {
sleepsAreBad();
driver.switchTo().frame("ContainerFrame");
driver.switchTo().frame("iframeCommunityContainer");
driver.switchTo().frame("FORMCONTAINER");
fluentWait(By.id("StartDate_txtInput"));
firstDayOfLeaveInput.sendKeys(firstDate);
sleepsAreBad();
lastDayofLeaveInput.sendKeys(lastDate);
try {
submitButton.click();
} catch (UnhandledAlertException f) {
try {
sleepsAreBad();
driver.switchTo().defaultContent();
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
} catch (NoAlertPresentException e) {
e.printStackTrace();
}
}
sleepsAreBad();
return PageFactory.initElements(driver, FirstPage.class);
}
private void sleepsAreBad() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public class BaseTest {
public static WebDriver driver;
static String driverPath = "C:\\";
@BeforeClass
public static void setUp() {
System.out.println("****************");
System.out.println("launching Browser");
System.out.println("****************");
// Browser selection
//Firefox
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
driver = new FirefoxDriver(dc);
driver.get(URL);
@AfterClass()
public static void tearDown() {
if (driver != null) {
System.out.println("Closing browser");
driver.quit();
}
}
public class Bookings extends BaseTest{
@Test(description = "Holiday booking")
public void CD01() {
FirstPage firstPage = PageFactory.initElements(driver, FirstPage.class);
firstPage
.logIn("username", "password")
.clickHolidayLink()
.completeHolidayFormAndSubmit("12/05/2016", "15/05/2016");
}
alert box 这是警告框
答案 0 :(得分:2)
在UnhandledAlertException
catch
后尝试此操作
WebDriverWait wait = new WebDriverWait(driver, 3000);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
alert.accept();
可能会帮助你...... :)。
答案 1 :(得分:0)
对于大多数警报,您可以按Enter键按警报"确定"。