以使用百度搜索为例,编写第一个基于java的webdriver脚本

1.下面以最简单的baidu搜索为例,来编写第一个java的webdriver脚本

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SearchByBaidu {

public static void main(String[] args) {

// 定义firefox驱动程序gecko.driver的安装路径
System.setProperty("webdriver.gecko.driver", "D:\\Tools\\geckodriver-v0.11.1-win64\\geckodriver.exe");

// 定义firefox的安装路径,注意当firefox安装时,用户修改过默认安装路径,则此时必须手工指定firefox的安装路径,否则程序运行时,无法启动firefox
System.setProperty("webdriver.firefox.bin","D:\\firefox\\firefox.exe");

WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");


//最大化window窗口
driver.manage().window().maximize();

WebElement txtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("Glen");
WebElement btn = driver.findElement(By.id("su"));
btn.click();
driver.close();
}
}

2. 此时会在eclipse里面出现一个很特别的错误:The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)

并且eclipse各个包的java文件图标都有小红叉。

仔细检查脚本,发现脚本中sendKeys方法传入参数都合法,该语句并没有错误。

 

后来自己百度了一下,原来是Java Compiler版本太低了,默认是1.4.只需要将compiler改为1.7 就不会报这个错了。

方法:Right click on your java project and select Build Path -> Click on Configure Build Path>In project properties window: Click/select Java Compiler at the left panel

At the right panel: change the Compiler compliance level from 1.4 to 1.7 or higher>Lastly Click on Apply and OK

 

此时eclipse各个包的java文件图标的小红叉也自动消失了

 

转载于:https://www.cnblogs.com/alliefu/p/6548866.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值