
//操作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);//釋放該鍵 }
