selenium 3 java 元素高亮顯示
阿新 • • 發佈:2018-11-01
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(); } }
執行結果 如圖 :