天天看點

selenium 彈窗、iframe處理方法

1、Alert類

  1. Alert是指windows彈窗的一些操作,需要new一個Alert類
  2. driver.switchTo().alert():切換到alert視窗
  3. alert.getText():取得彈窗上面的字元串
  4. alert.accept():點選确定/ok類的按鈕,使彈窗消失
  5. alert.dismiss():取消
selenium 彈窗、iframe處理方法
public void testAlert(){
        WebElement element = driver.findElement(By.className("alert"));
        element.click();
        Alert alert = driver.switchTo().alert();    
        String text = alert.getText();                
        alert.accept();
        //alert.dismiss();
        System.out.println(text);
    }      
selenium 彈窗、iframe處理方法

 使用Actions類

  1. 先要new一個Actions的類對象
  2. 最後的perform()一定要加上,否則執行不成功
public void testAlertByActions(){
        WebElement element = driver.findElement(By.className("alert"));
        Actions action = new Actions(driver);
        action.click(element).perform();
        Alert alert = driver.switchTo().alert();
        String text = alert.getText();
        alert.accept();
        //alert.dismiss();
        System.out.println(text);
    }      

 2、Action類

  1. 先要new一個Actions的類對象
  2. 最後的perform()一定要加上,否則執行不成功
public void testActions(){
        WebElement element = driver.findElement(By.className("over"));
        Actions action = new Actions(driver);
        action.moveToElement(element).perform();
        String text = driver.findElement(By.id("over")).getText();
        System.out.println(text);        
    }      

 3、調用JS 

  1. 一般用來執行一段JS,來改變HTML
  2. 一些非标準控件無法用selenium2的API時,可以執行JS的辦法來取代
  3. executeScript這個方法的參數為字元串,為一段JS代碼
public void testJS(){
        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("alert('helloworld')");
    }      

 4、Wait機制及實作

  • 在規定的時間内隻要符合條件即傳回,下面的代碼中是隻要isDisplayed即傳回
public void testWait(){
        WebElement waitButton = driver.findElement(By.id("wait"));
        waitButton.click();
        boolean flag = new WebDriverWait(driver, 10).until
                (
                        new ExpectedCondition<Boolean>() 
                        {
                            public Boolean apply(WebDriver driver) 
                            {
                                return driver.findElement(By.className("red")).isDisplayed();
                            }     
                        }
                );
        if(flag){
            String text = driver.findElement(By.className("red")).getText();
            System.out.println(text);    
        }
    }      

 5、Iframe操作

  1. 如果iframe标簽有能夠唯一确定的id或者name,就可以直接用id或者name的值:driver.switchTo().frame("aa");
  2. 如果iframe标簽沒有id或者name,但能夠通過頁面上确定其是第幾個(也就是通過index來定位iframe,index是從0開始的):driver.switchTo().frame(0);
  3. 還可以通過xpath的方式來定位iframe,寫法如下:
  1. WebElement iframe = driver.findElement(By.xpath("//iframe[@name='aa']"));
  2. driver.switchTo().frame(iframe);
public void testIFrame(){
        driver.findElement(By.id("user")).sendKeys("test");
        driver.switchTo().frame("aa");
        driver.findElement(By.id("user")).sendKeys("iframe test");
        driver.switchTo().defaultContent();//傳回頂層frame
        driver.findElement(By.id("user")).sendKeys("---new test");
    }      

 6、多視窗切換

  1. gettWindowHandles:取得driver所打開的所有的頁面的句柄
  2. witchTo是指切換到相應的視窗中去,window中的參數是指要切過去的視窗的句柄
public void testMultiWindow(){
        driver.findElement(By.id("user")).sendKeys("test");
        String handle = driver.getWindowHandle();//擷取目前視窗的句柄
        System.out.println(handle);
        WebElement element = driver.findElement(By.className("open"));
        element.click();
        Set<String> handles = driver.getWindowHandles();
        for(String s : handles)
        {
            if(!s.equals(handle))
            {
                System.out.println(s);
                driver.switchTo().window(s);
                driver.findElement(By.id("kw")).sendKeys("glen");
            }
        }
        
        driver.switchTo().window(handle);
        driver.findElement(By.id("user")).sendKeys("---new test");        
    }      

作者:​​Glen.He​​

繼續閱讀