package bwgang; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import OpenBrowserConf.OpenBrowserConf; public class Login { private WebDriver dr; private String url; private WebElement el; public void openBrowser(){ url = "http://www.baidu.com"; dr= (new OpenBrowserConf()).openChrome(); dr.get(url); } public void clickLoginLink() throws InterruptedException{ this.openBrowser(); el = dr.findElement(By.linkText("登陸")); el.click(); //等待登陸 窗口 (new WebDriverWait(dr,10)).until( new ExpectedCondition<Boolean>(){ public Boolean apply(WebDriver d){ return d.findElement(By.id("TANGRAM__PSP_2__body")).isDisplayed(); } }); } public void login(){ this.dr.findElement(By.id("TANGRAM__PSP_8__userName")).clear(); this.dr.findElement(By.id("TANGRAM__PSP_8__userName")).sendKeys("XXXX"); this.dr.findElement(By.id("TANGRAM__PSP_8__password")).clear(); this.dr.findElement(By.id("TANGRAM__PSP_8__password")).sendKeys("XXXX"); this.dr.findElement(By.id("TANGRAM__PSP_8__submit")).click(); } public static void main(String[] arg) throws InterruptedException{ Login login = new Login(); login.clickLoginLink(); login.login(); } }