????1??WebDriver?????Щ????

import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class AlertOperate {
 static WebDriver dr = new InternetExplorerDriver();
 
 public static void main(String args[]) throws InterruptedException{
  dr.get("www.baidu.com");
 
  dr.findElement(By.id("lb")).click();
 
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  tanchukuang();
 }
 
 
 //????????1??alert??javascript??????
 public boolean dealPotentialAlert(WebDriver driver??boolean option) {
  boolean flag = false;
  try {
   Alert alert = driver.switchTo().alert();
   if (null == alert)
    throw new NoAlertPresentException();
   try {
    if (option) {
     alert.accept();
     System.out.println("Accept the alert: " + alert.getText());
    } else {
     alert.dismiss();
     System.out.println("Dismiss the alert: " + alert.getText());
    }
    flag = true;
   } catch (WebDriverException ex) {
    if (ex.getMessage().startsWith("Could not find"))
     System.out.println("There is no alert appear!");
    else
     throw ex;
   }
  } catch (NoAlertPresentException e) {
   System.out.println("There is no alert appear!");
  }
  return flag;
 }
 
 //?????JS????
 public static boolean testNewWindow(){
  //?????????
   String currentHandle = dr.getWindowHandle();
  //??????д??????
   Set<String> handles = dr.getWindowHandles();
   handles.remove(currentHandle);
   if (handles.size() > 0) {
    try{
     dr.switchTo().window(handles.iterator().next());
     //dr.switchTo().window(dr.getWindowHandles().iterator().next());
     return true;
    }catch(Exception e){
     System.out.println(e.getMessage());
     return false;
    }
   }
   System.out.println("Did not find window");
   return false;
 }
 
 //?????????
 public static void tanchukuang() throws InterruptedException{
  //??????д???
  Set<String> allWindowsId = dr.getWindowHandles();
  //?????????????????μ????
  for(String windowId : allWindowsId){
   dr.switchTo().window(windowId);
   Thread.sleep(1000);
   dr.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys("test");
  
   //????????????????
   //dr.findElement(By.xpath("//button[@type='button']")).click();
   //System.out.println(dr.switchTo().window(windowId).getTitle());
   break;
  }
 }
 
}