码迷,mamicode.com
首页 > 其他好文 > 详细

Selenium驱动Chrome浏览器

时间:2018-01-20 16:20:13      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:assert   百度首页   new   log   resources   自动   equals   搜索   path   

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Chrome {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
String baiduHomePage;
baiduHomePage = "https://www.baidu.com/";
//百度首页的地址

WebDriver driver;
//声明一个WebDriver
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
//消除“Chrome正受到自动测试软件的控制”提示
options.addArguments
("--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
//消除“不安全”提示,启用用户默认的数据目录
driver =new ChromeDriver(options);
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
}
}

需要注意的是,
Chrome浏览器可以是官网上最新的版本,
驱动的版本是:
chromedriver_win32

Selenium驱动Chrome浏览器

标签:assert   百度首页   new   log   resources   自动   equals   搜索   path   

原文地址:https://www.cnblogs.com/yjlch1016/p/8320885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!