准备浏览器驱动
例如,在D盘下新建文件夹BrowserDriver,然后将下载好的浏览器驱动统一放在该文件夹下管理。
java selenium启动浏览器,例如:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoginTest {
public WebDriver driver;
public String baseUrl = "http://baidu.com/";
@BeforeMethod
public void beforeMethod() throws Exception{
System.setProperty("webdriver.gecko.driver", "D:\\BrowserDriver\\geckodriver.exe");
// firefox版本>48,且没有安装在默认位置,需要指明安装路径
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
Thread.sleep(5000);
}
启动其他浏览器的方式如下:
Firefox浏览器
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver","D:\\BrowserDriver\\geckodriver.exe");
// firefox版本>48,且没有安装在默认位置,需要指明安装路径
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
Chrome浏览器
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver","D:\\BrowserDriver\\chromedriver.exe");
driver = new ChromeDriver();
Chrome浏览器 headless模式
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
System.setProperty("webdriver.chrome.driver","D:\\BrowserDriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setHeadless(Boolean.TRUE);
driver = new ChromeDriver(options);
IE浏览器
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
System.setProperty("webdriver.ie.driver","D:\\BrowserDriver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
网友评论