seleium+java自动化,启动Firefox、Chrome、IE浏览器

一、启动firefox浏览器的方法

1、下载浏览器驱动

下载地址

http://www.seleniumhq.org/download/
https://github.com/mozilla/geckodriver/releases

2、将下载好的驱动文件进行解压,保存到一个指定的目录,注意:路径不要太深

3、脚本中编写启动浏览器的关键语句:

脚本中"D:\\BrowserDriver\\geckodriver.exe"是第2小节下载保存驱动的路径

public class OperBrowses {
	
	public static void main(String[] args) {
//		获取驱动地址
		System.getProperty("webdriver.gecko.driver", "D:\\BrowserDriver\\geckodriver.exe");
//		创建一个叫driver的对象,启动火狐浏览器
		WebDriver driver = new FirefoxDriver();

4、使用import关键字引入相关的jar包

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

二、启动Chrome、IE浏览器

步骤与启动Firefox浏览器一样

使用谷歌浏览器步骤:
1. 下载不Chrome浏览器版本对应的chromedriver
官方下载地址:http://docs.seleniumhq.org/download/
国内可用地址:http://chromedriver.storage.googleapis.com/index.html
解压下载的.zip文件得到chromedriver.exe
2. 将chromedriver.exe放到电脑磁盘某个路径中
3. 在脚本中通过System.setProperty方法指定chromedriver的地址

System.setProperty("webdriver.chrome.driver", "D:\\BrowserDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

4. 使用import关键字引入相关的jar包

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

 WebDriver使用启动 IE浏览器步骤:
1. 下载IE driver最新的版本
国内可用地址:http://selenium-release.storage.googleapis.com/index.html
本课程使用版本:http://selenium-release.storage.googleapis.com/index.html?path=3.8/
解压下载的.zip文件得到IEDriverServer.exe
2. 将IEDriverServer.exe放到电脑磁盘某个路径中
3. 在脚本中通过System.setProperty方法指定IEDriver的地址

System.setProperty("webdriver.ie.driver", "D:\\BrowserDriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();

4. 使用import关键字引入相关的jar包

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

猜你喜欢

转载自blog.csdn.net/qq_41650233/article/details/84473905