一、启动浏览器
Selenium API :
https://seleniumhq.github.io/selenium/docs/api/java/index.html
1、启动Firefox浏览器
。准备工作
- 下载firefox驱动geckodriver(注意浏览器的版本)
- 火狐浏览器的版本
package cn.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenBrower_Firefox {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.gecko.driver", "libs/geckodriver.exe");
driver = new FirefoxDriver();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void openBrower() throws Exception {
driver.get("https://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("你好");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(2000);
}
}
。启动本地机的Firefox配置
因为WebDriver启动firefox时是启动一个全新的浏览器,我们又想用到自己下载的插件,就可以使用如下的方法启动本地机的profile配置,也就是说我们先设置好需要用到的插件到firefox里面,然后启动WebDriver,就可以直接使用了。
- 设置firefox的profile
Win+R---输入firefox.exe -p----回车
可以自己常见配置文件
- 代码如下
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
public class OpenBrower_Firefox {
private WebDriver driver;
@Before
public void setUp() {
ProfilesIni pi=new ProfilesIni();
FirefoxProfile profile=pi.getProfile("selenium");
System.setProperty("webdriver.gecko.driver", "libs/geckodriver.exe");
FirefoxOptions options=new FirefoxOptions();
options.setProfile(profile);
driver = new FirefoxDriver(options);
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void openBrower() throws Exception {
driver.get("https://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("你好");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(2000);
}
}
2、启动Chrome浏览器
。Chrome浏览器的驱动下载地址
public class OpenBrower_Chrome {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "libs\\chromedriver.exe");
driver=new ChromeDriver();
}
@Test
public void testOpen() throws Exception {
driver.get("https://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).sendKeys("八戒");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(2000);
}
@After
public void tearDown() {
driver.close();
}
}
3、启动IE浏览器
。IE浏览器的驱动下载地址
。配置IE浏览器的Internet选项
- 工具-Internet选项-安全
- 2、在Internet,本地Internet,受信任的站点,受限制的站点中去掉《启用保护模式》复选中的“√”
-
3、点击“确定”,即可
public class OpenBrower_IE {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.ie.driver", "libs\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.out.println("@Before");
}
@After
public void tearDown() {
driver.quit();
System.out.println("@After");
}
@Test
public void testDemo() throws Exception {
driver.get("https://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).sendKeys("八戒");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(2000);
System.out.println("@Test");
}
@Test
public void testDemo2() throws Exception {
driver.get("https://www.baidu.com/");
Thread.sleep(2000);
driver.findElement(By.id("kw")).sendKeys("八戒");
Thread.sleep(2000);
driver.findElement(By.id("su")).click();
Thread.sleep(2000);
System.out.println("@Test");
}
}
- 如果不设置IE的话,会出现如下错误
org.openqa.selenium.NoSuchWindowException: Unable to find element on