Java编程语言下 Selenium 驱动各个浏览器代码
这里采用的是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 驱动各个浏览器代码的更多相关文章
- Java编程语言下Selenium驱动各个浏览器代码
这里采用的是Selenium3.7版本,首先介绍的是在Windows环境下运行的: 总结下注意事项: 1,设置各个浏览器的Driver路径 System.setProperty("" ...
- Java编程语言下Selenium 利用Robot自己编写截屏方法
import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.concurrent.TimeUnit; import ...
- Java编程语言下Selenium 鼠标悬停以及右击操作
// 基于Actions类创建一个对象 Actions action = new Actions(driver); // 鼠标悬停在药渡公司全称字段上 action.moveToElement(Yao ...
- Java环境下 selenium webDriver + chrome浏览器搭建与调试
一.首先下载selenium webDriver jar包,下载地址如下: http://selenium-release.storage.googleapis.com/index.html 二.下载 ...
- Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作
WebElement selector = driver.findElement(By.id("Selector")); Select select = new Select(se ...
- selenium驱动chrome浏览器问题
selenium是一个浏览器自动化测试框架,以下介绍其如何驱动chrome浏览器? 1.下载与本地chrome版本对应的chromedriver.exe ,下载地址为http://npm.taobao ...
- (原创)如何使用selenium 驱动chrome浏览器并且打开方式为手机模式-转载请注明出处
随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要. 对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊. 使用chrome dr ...
- Java 编程下 Eclipse 如何设置单行代码显示的最大宽度
Eclipse 下一行代码的默认宽度是 80 , 稍长一点的一行代码就会自动换行,代码可读性较差,我们可以自己在 Eclipse 对代码宽度进行设置. 设置路径为:[Window]→[Preferen ...
- Selenium驱动Chrome浏览器
import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome ...
随机推荐
- PAT Waiting in Line[转载]
//转自:https://blog.csdn.net/apie_czx/article/details/45537627 1014 Waiting in Line (30)(30 分)Suppose ...
- mysql 简单主从
主服务器master [root@localhost ~]# vim /etc/my.cnf [mysqld] log-bin=mysql-bin #必须开启log-bin server-id=129 ...
- 浅谈远程登录时,ssh的加密原理
SSH:Secure Shell,是一种网络安全协议,主要用于登录远程计算机的加密过程. 登录方式主要有两种: 1.基于用户密码的登录方式: 加密原理: 当服务器知道用户请求登录时,服务器会把 ...
- [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- !! A股历史平均市盈率走势图
http://value500.com/PE.asp 一. A股历史平均市盈率走势图 *数据来源:上海证券交易所 分享到: 354 - 上海A股 深圳A股更新时间 2017年6月7日 2017年6月7 ...
- js中数组的字符串表示
<html> <head> <title>数组的字符串表示</title> <script type="text/javascript& ...
- 概率检索模型:BIM+BM25+BM25F
1. 概率排序原理 以往的向量空间模型是将query和文档使用向量表示然后计算其内容相似性来进行相关性估计的,而概率检索模型是一种直接对用户需求进行相关性的建模方法,一个query进来,将所有的文档分 ...
- 20154312 曾林 Exp8 web基础
1.基础问题回答 1.1.什么是表单 1.2.浏览器可以解析运行什么语言 1.3.WebServer支持哪些动态语言 2.实践总结与体会 3.实践过程记录 ----3.1.Web前端:HTML基础 - ...
- charles破解
替换安装路径->Charles\lib下的charles.jar文件成破解版jar文件,如果再次启动未弹出30天试用的提示,说明破解成功 charles:https://pan.baidu.co ...
- python-正则表达式练习题
1.匹配一行文字中的所有开头的字母内容 #coding=utf-8 import re s="i love you not because of who you are, but becau ...