Selenium驱动Firefox浏览器

用Maven构建Selenium依赖:

 

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>

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

public class Firefox {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.firefox.marionette", "src/main/resourcec/geckodriver.exe");
//指定Firefox浏览器驱动的路径,使用相对路径
String baiduHomePage;
baiduHomePage = "https://www.baidu.com/";
//百度首页的地址

WebDriver driver;
//声明一个WebDriver
driver = new FirefoxDriver();
driver.manage().window().maximize();
//使浏览器窗口最大化
driver.get(baiduHomePage);
//打开百度
Thread.sleep(2000);
//强制线程等待2秒钟
assert driver.getTitle().equals("百度一下,你就知道");
//断言页面标题

driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("Selenium");
//在百度搜索输入框输入“Selenium”
driver.findElement(By.xpath(".//*[@id='su']")).click();
//点击搜索按钮
Thread.sleep(2000);
assert driver.getTitle().equals("Selenium_百度搜索");

driver.close();
//关闭浏览器窗口
driver.quit();
//结束dirver
}
}

需要注意的是,
Firefox浏览器不能是官网上最新的版本,
否则会出现启动了浏览器,
却无法打开网址的情况;
我用的版本是:
Firefox-v52.5.3-win64
驱动的版本是:
geckodriver-v0.19.1-win64
 


转载于:https://www.cnblogs.com/yjlch1016/p/8320792.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值