用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
 


内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!