首页 > 其他 > 详细

Selenium驱动Chrome浏览器

时间:2018-01-20 15:35:26      阅读:249      评论:0      收藏:0      [点我收藏+]
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浏览器

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!