之前用过selenium的python模块,其实selenium也有java的api
常用功能实现总结
获得浏览器的驱动,比如ie浏览器
public class SeleniumConfig {
private static WebDriver driver;
static {
System.setProperty("webdriver.ie.bin", "C:/Python34/selenium/webdriver/ie/IEDriverServer.exe");
DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
dc.setCapability("ignoreProtectedModeSettings", true);
driver = new InternetExplorerDriver(dc);
}
public static WebDriver getDriver() {
return driver;
}
}
打开网页
driver.get("网址");
根据id获得控件,并进行一些操作
WebElement useridInput = driver.findElement(By.id("USR_ID"));
useridInput.sendKeys("admin");
根据DOM结构获得控件
WebElement input = driver.findElement(By.xpath("//object[@id='SSClient']"));
((JavascriptExecutor) driver).executeScript(
"var object = document.getElementById(\"SSClient\"); " +
"object.output3 = 9; " +
"object.output28 = \"BGG8uf/WnRqnBwvvV2bnt+dCS456B/sSCXLTWmmzgtXDKkKzYrO4SqIeFzTxWWGUJizmwnLKmsySce47qrAZhGC0eNBRw6aP3M7S+mUhPWTxkYk6IwEh+k2E824eG6VlCMAEmLulsmkLqAupxNVXHBZ6/n4FtZJBVW72xDYIhCAZIlLwXosPWjkv/BFDOe66r7sovN7zQh0oZo23zOLXwnrTEsd4obCXt+UUT2BN5l2Jt2a5iIFK9/Gg7L2FZ9XpoP/WzaXfNJ31QkbFe47qVIc7nbeQQuCgdz5kCk0yMEseIkr/pDZ+YWx877fqDxI2hG/ZbsTV4xxnxWHhKlwKYQ==\"; " +
"arrOcxId = []; " +
"arrOcxId.push(\"SSClient\"); " +
"arrOcxHiddenId = []; " +
"arrOcxHiddenId.push(\"USR_PWD\"); " +
"var password = GetPassGuardCtrlKeyCodeChar(\"SSClient\"); ");