时间:2020-10-01 15:47:15 | 栏目:JAVA代码 | 点击:次
WebDriver实现自动化打开IE中的google网页并实现搜索,需要导入WebDriver相关的jar包,设置好环境变量。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class TestWebDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver", "C:\\testwebdriver\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.get("http://www.google.com.hk/");
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("The current url is:" + driver.getCurrentUrl());
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("days");
WebElement elementBtn = driver.findElement(By.xpath("//input[@name='btnG']"));
elementBtn.click();
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.close();
}
}