package code; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; public class First { public static void main(String[] args) { /* * 设置指定键对值的系统属性 * setProperty (String prop, String value); * * 参数: * prop - 系统属性的名称。 * value - 系统属性的值。*/ //设置ie浏览器本地地址 //System.setProperty("webdriver.ie.driver","D://softwares//webdriver//IEDriverServer.exe"); //设置ie浏览器项目地址 System.setProperty("webdriver.ie.driver","source//webdriver//IEDriverServer.exe"); //创建一个浏览器实例 WebDriver driver = new InternetExplorerDriver(); //通过浏览器输入百度地址 driver.get("http://www.baidu.com/"); //获取当前页面title属性 System.out.println(driver.getTitle()); WebElement seek = (WebElement) driver.findElement(By.id("kw")); WebElement su = (WebElement) driver.findElement(By.id("su")); seek.sendKeys("上海天气"); su.click(); System.out.println(seek.getAttribute("value")); //页面停留2秒 try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.navigate().to("http://news.baidu.com/"); driver.close(); } }
其中采用的是IE11浏览器启动,启用是需修改配置文件,相关方法如下:
For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. (这段告诉你需要修改注册表。)
For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. (32bit Windows看这里。)
For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.(64bit Windows看这里。)
翻译过来的意思即,修改你的注册表(Run->regedit->Enter),路径如下:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
如果FeatureControl下没有FEATURE_BFCACHE,就以FEATURE_BFCACHE为名new一个key!并在其下创建一个DWORD,取名为:iexplore.exe,value为0。