selenium入门demo(需启动浏览器)

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。

posted on 2018-05-25 16:23  zero_hu 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zero-hu/p/9089236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值