首页 > 代码库 > selenium高亮显示操作步骤方法
selenium高亮显示操作步骤方法
package com.allin.pc;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class highlight {
WebDriver driver;
String baseUrl;
JavascriptExecutor js;
public static void sleep(double d){
try {
d *= 1000;
Thread.sleep((int)d);
} catch (Exception e) {
// TODO: handle exception
}
}
@BeforeClass
public void setUp(){
baseUrl = "http://www.sogou.com";
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(baseUrl);
}
@AfterClass
public void tearDown(){
driver.close();
}
/**
* 高亮显示操作步骤
*/
@Test
public void getHighlight(){
WebElement searchInput = driver.findElement(By.xpath(".//*[@id=‘query‘]"));
WebElement searchBtn = driver.findElement(By.xpath(".//*[@id=‘stb‘]"));
//调用高亮显示元素的封装函数,将搜索输入框进行高亮
highlight(searchInput);
searchInput.sendKeys("光荣之路自动化测试");
sleep(5);
highlight(searchBtn);
sleep(4);
searchBtn.click();
sleep(3);
}
public void highlight(WebElement element){
JavascriptExecutor js = (JavascriptExecutor) driver;
//使用Javascript语句将传入参数的页面元素对象的背景颜色和边框颜色分别设定黄色和红色
js.executeScript("arguments[0].setAttribute(‘style‘, arguments[1]);",
element,"background: yellow; border: 2px solid red;");
}
}
selenium高亮显示操作步骤方法