mport org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BaiduDemo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\driver\\chromedriver.exe"); //指定驱动路径
WebDriver driver =new ChromeDriver();
driver.get("http://www.baidu.com");
//定位并点击登录.
driver.findElement ( By.xpath ( "//*[@id='u1']/a[7]" ) ).click ();
Thread.sleep ( 1000 );
//出现的扫码页面登录 定位并点击选择用户登录
driver.findElement ( By.xpath ( ".//*[@id='TANGRAM__PSP_10__footerULoginBtn']" ) ).click ();
//定位用户名文本框
WebElement userName = driver.findElement(By.id("TANGRAM__PSP_10__userName"));
userName.sendKeys("test username");
//定位密码文本框
WebElement password = driver.findElement(By.id("TANGRAM__PSP_10__password"));
password.sendKeys("123456");
//创建一个JavascriptExecutor对象
JavascriptExecutor js =(JavascriptExecutor) driver;
//用户名文本框设置颜色 ..
js.executeScript ( "arguments[0].setAttribute('style', arguments[1]);",userName,"background: orange; border: 2px solid red;");
Thread.sleep ( 2000 );
//密码文本框设置颜色
js.executeScript ( "arguments[0].setAttribute('style', arguments[1]);",password,"background: yellow; border: 2px solid red;");
Thread.sleep ( 2000 );
driver.quit();
}
}
运行结果 如图 :