这里采用的是Selenium3.7版本,首先介绍的是在Windows环境下运行的;

总结下注意事项:

1,设置各个浏览器的Driver路径

System.setProperty("","");

2,创建一个浏览器对象

WebDriver driver = new xxx();

1. 驱动IE浏览器

IE浏览器的驱动有32位和64位,两个版本,建议使用32的IEdriver,因为64位的IE driver 跑起来实在太慢了。

注意IE的保护模式,在各个状态下,全部禁用。如下截图,4个模式下,保护模式全部不勾选

 package base;

 import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver; public class TestIEDriver { public static void main(String args[]) throws InterruptedException { System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("http://www.baidu.com");
String s = driver.getTitle();
System.out.print(s);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(1000);
driver.close(); }
}

2. 驱动Chrome浏览器

 package base;

 import java.util.Arrays;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities; public class TestChromeDriver { public static void main(String args[]) throws InterruptedException { System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
options.addArguments("--test-type", "--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.baidu.com"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(1000);
driver.close(); } }

3. 驱动火狐FireFox浏览器

 package base;

 import java.util.concurrent.TimeUnit;

 import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class TestFireFoxDriver { public static void main(String args[]) throws InterruptedException { System.setProperty("webdriver.Firefox.driver", ".\\Tools\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.baidu.com");
String s = driver.getTitle();
System.out.print(s);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(1000);
driver.close(); } }

浏览器驱动的下载地址

http://npm.taobao.org/mirrors/chromedriver/

http://chromedriver.storage.googleapis.com/index.html

https://github.com/mumuy/data_location

常见的错误汇总

1. org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Browser zoom level was set to 75%. It should be set to 100% (WARNING: The server did not provide any stacktrace information)

ie浏览器显示百分比,需要调节到100%

2. org.openqa.selenium.ElementNotVisibleException: The point at which the driver is attempting to click on the element was not scrolled into the viewport. (WARNING: The server did not provide any stacktrace information)

不同设备,窗口大小问题,页面没有显示全,需要向下滚动

3.org.openqa.selenium.SessionNotCreatedException: session not created exception: Chrome version must be >= 66.0.3359.0

Chrome浏览器驱动版本不匹配的问题,需要查询目前chrome版本,下载匹配的驱动

4.No such Element Exception
元素定位的方式有误,原因:页面源码改动
原因: 元素还不是可用的状态
原因: 出现弹窗

Java编程语言下 Selenium 驱动各个浏览器代码的更多相关文章

  1. Java编程语言下Selenium驱动各个浏览器代码

    这里采用的是Selenium3.7版本,首先介绍的是在Windows环境下运行的: 总结下注意事项: 1,设置各个浏览器的Driver路径 System.setProperty("" ...

  2. Java编程语言下Selenium 利用Robot自己编写截屏方法

    import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.concurrent.TimeUnit; import ...

  3. Java编程语言下Selenium 鼠标悬停以及右击操作

    // 基于Actions类创建一个对象 Actions action = new Actions(driver); // 鼠标悬停在药渡公司全称字段上 action.moveToElement(Yao ...

  4. Java环境下 selenium webDriver + chrome浏览器搭建与调试

    一.首先下载selenium webDriver jar包,下载地址如下: http://selenium-release.storage.googleapis.com/index.html 二.下载 ...

  5. Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作

    WebElement selector = driver.findElement(By.id("Selector")); Select select = new Select(se ...

  6. selenium驱动chrome浏览器问题

    selenium是一个浏览器自动化测试框架,以下介绍其如何驱动chrome浏览器? 1.下载与本地chrome版本对应的chromedriver.exe ,下载地址为http://npm.taobao ...

  7. (原创)如何使用selenium 驱动chrome浏览器并且打开方式为手机模式-转载请注明出处

    随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要. 对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊. 使用chrome dr ...

  8. Java 编程下 Eclipse 如何设置单行代码显示的最大宽度

    Eclipse 下一行代码的默认宽度是 80 , 稍长一点的一行代码就会自动换行,代码可读性较差,我们可以自己在 Eclipse 对代码宽度进行设置. 设置路径为:[Window]→[Preferen ...

  9. Selenium驱动Chrome浏览器

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

随机推荐

  1. Fisher精确检验【转载】

    转自:https://en.wikipedia.org/wiki/Fisher%27s_exact_test https://www.cnblogs.com/Dzhouqi/p/3440575.htm ...

  2. $scope.$apply

    对于一个在前端属于纯新手的我来说,Javascript都还是一知半解,要想直接上手angular JS,遇到的阻力还真是不少.不过我相信,只要下功夫,即使是反人类的设计也不是什么大的问题. Okay, ...

  3. [lr & ps] 色彩空间管理

    色彩空间 • 定义 色彩空间,Color Space,又称作色域.在色彩学中,人们建立了许多色彩模型,以一维.二维.三维甚至四维空间坐标来表示某一色彩,这种坐标系统所能定义的色彩范围即色彩空间.我们经 ...

  4. location对象查询字符串参数

    虽然location.search可以返回从问号到URL末尾的所有内容,但却没有办法逐个访问其中的每个查询字符串参数.为此,可以创建下面这样一个函数,用以解析查询字符串,然后返回包含所有参数的一个对象 ...

  5. HDU 5059 Help him(简单模拟题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那 ...

  6. js自执行函数的常见写法

    js自执行函数的常见写法 2016-12-20 20:02:26 1.关于自执行函数 1.1 写自执行函数的好处:独立的作用域,不会污染全局环境 (function() { })(); 1.2 理解重 ...

  7. VS2010/MFC编程入门之三十一(常用控件:树形控件Tree Control 下)

    前面一节讲了树形控件Tree Control的简介.通知消息以及相关数据结构,本节继续讲下半部分,包括树形控件的创建.CTreeCtrl类的主要成员函数和应用实例. 树形控件的创建 MFC为树形控件提 ...

  8. uva11354 LCA+最小生成树+dp

    源自大白书 题意 有n座城市通过m条双向道路相连,每条道路都有一个危险系数.你的任务是回答若干个询问,每个询问包含一个起点s和一个终点t,要求找到一条从s到t的路,使得途径所有的边的大最大危险系数最小 ...

  9. linux 判断文件最后更新时间 实现监控日志是否有输出功能

    linux 判断文件最后更新时间 实现监控日志是否有输出功能. 需求:监控log.txt日志文件,超过一分钟没输出内容就认为是停了,则自动启动程序. 用stat 可以看文件的更新时间stat -c % ...

  10. git常用命令3

    一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态git commit 提交git branch -a 查看所有的分支git branch -r 查看 ...