selenium webdriver操作各浏览器
描述
本文主要是针对Chrome 62 , firefox57 ,和IE11 三个版本的操作。相关的driver 。可点击以下链接。所有的driver 建议放在浏览器的目录下,本文中所有的driver都放置在对应的浏览器根目录下。
https://files.cnblogs.com/files/linxinmeng/IE11_win10_3.5.zip
https://files.cnblogs.com/files/linxinmeng/Firefox57_win10_java1.8.zip
https://files.cnblogs.com/files/linxinmeng/chromedriver_X64_Chrome62.zip
注意事项
IE浏览器需要对安全进行设置 。以及【设置】中的缩放必须是100%。
1 package 票务系统前台; 2 3 import java.util.concurrent.TimeUnit; 4 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.openqa.selenium.chrome.ChromeOptions; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 import org.openqa.selenium.firefox.FirefoxOptions; 10 import org.openqa.selenium.firefox.FirefoxProfile; 11 import org.openqa.selenium.firefox.internal.ProfilesIni; 12 import org.openqa.selenium.ie.InternetExplorerDriver; 13 import org.openqa.selenium.remote.DesiredCapabilities; 14 15 public class 浏览器操作 { 16 static WebDriver driver; 17 18 public static void main(String[] args) { 19 // IE_open(driver); 20 // firefox_Open1(driver); 21 chrome_Open(driver); 22 24 } 25 26 public static WebDriver chrome_Open(WebDriver driver) { 27 System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver_X64\\chromedriver_X64.exe"); 28 ChromeOptions Options = new ChromeOptions(); 29 Options.addArguments("user-data-dir=C:\\Users\\linxinmeng\\AppData\\Local\\Google\\Chrome\\User Data"); 30 driver = new ChromeDriver(Options); 31 driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS); 32 33 driver.get("http://www.baidu.com"); 34 return driver; 35 } 36 37 public static WebDriver IE_open(WebDriver driver) { 38 System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer.exe"); 39 driver = new InternetExplorerDriver(); 40 driver.get("http://www.baidu.com"); 41 return driver; 42 43 } 44 45 public static WebDriver firefox_Open1(WebDriver driver) { 46 47 System.setProperty("webdriver.firefox.bin", "D:\\软件应用宝\\Mozilla Firefox\\firefox.exe"); 48 System.setProperty("webdriver.gecko.driver", "D:\\软件应用宝\\Mozilla Firefox\\geckodriver.exe"); 49 FirefoxOptions ffo = new FirefoxOptions(); 50 driver = new FirefoxDriver(ffo); 51 driver.get("https://www.baidu.com"); 52 return driver; 53 } 54 55 }