java+selenium自動化遇到confirm彈窗,出現NoAlertPresentException: no alert open


//操作js的confirm彈窗,bool控制是否點擊確定,true為點擊確定,false為點擊取消
    public static void OperaterJSOfConfirm(WebDriver driver,String str,Boolean bool) throws InterruptedException, AWTException{
        WebElement button = driver.findElement(By.xpath(str));
        button.click();
        Thread.sleep(2000);
        /*Alert lert = driver.switchTo().alert();
        Thread.sleep(1000);//睡眠2″,否則可能會提示沒有打開的lert錯誤
        if(bool){
            lert.accept();
        }else{
            lert.dismiss();
        }*/
        //解決上面代碼經常出現的“no alert open”異常情況
        Robot robot = new Robot();//在基本屏幕坐標系中構造一個 Robot 對象
        robot.keyPress(KeyEvent.VK_SPACE);//按下給定的鍵
        robot.keyRelease(KeyEvent.VK_SPACE);//釋放該鍵
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM