zoukankan      html  css  js  c++  java
  • 13.selenium之警告框处理

    在 WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单,具体做法是使用switch_to_alert()方法定位到alert/confirm/prompt,然后使用text/accept/dismiss/sendKeys等方法进行操作。

    • getText(): 返回 alert/confirm/prompt 中的文字信息。
    • accept(): 接受现有警告框。
    • dismiss(): 解散现有警告框。
    • sendKeys(keysToSend): 发送文本至警告框。
    • keysToSend: 将文本发送至警告框。
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    
    public class AlertDemo {
    
        public static void main(String[] args) throws InterruptedException {
    
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.baidu.com");
    
    
            driver.findElement(By.xpath("//*[@id="s-usersetting-top"]")).click();
            Thread.sleep(2000);
    
            driver.findElement(By.xpath("//*[@id="s-user-setting-menu"]/div/a[1]")).click();
    
            Thread.sleep(2000);
            //保存设置
            driver.findElement(By.className("prefpanelgo")).click();
            Thread.sleep(2000);
            //接收弹窗
            driver.switchTo().alert().accept();
            Thread.sleep(2000);
    
            driver.quit();
        }
    }
  • 相关阅读:
    六个火柴搭四个三角形
    网络修复
    210. nginx root && alias区别
    206.pythondocx 操作word
    212. go语言堆实现
    211. django中的static 静态文件配置
    Java单元测试实战
    多测师肖老师__rf之变量(26.5)
    多测师肖老师__rf框架之定位方法(26.4)
    Failed to install texlive.infra
  • 原文地址:https://www.cnblogs.com/peiminer/p/13571031.html
Copyright © 2011-2022 走看看