课程: Test Automation with Selenium WebDriver for Java

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Alerts

Alerts

- [Instructor] Unlike modals that are embedded in the DOM, alerts are popup messages that are invoked by JavaScript. Since alerts are not part of the DOM, we can't access them using element locators. Fortunately, Selenium WebDriver offers methods specifically designed to interact with alerts. Let's write a script that gets the text from this alert. Our code has already clicked the link that triggers the alert, and to access this alert, we can say driver.switchTo.alert. And this returns an alert object. So let's assign that, Alert alert equals. Now we can get the message that appears within the alert by saying, String message equals alert.getText. We cannot continue doing anything with WebDriver until we handle this alert. So let's click the OK button. Now remember the alert is not in the DOM, so we can't use the click method here, but the alert class provides the accept method, which will click OK. So we say alert.accept. Now let's look at a confirmation alert, which requires the user…

内容