[個人環境]
- JDK 1.8.60
- selenium-2.47.1
- Eclipse Luna
- IE 9
[步驟教學]
- 至Selenuim官網有很多種程式語言供下載,考量未來執行環境我使用Java,下載.jar,然後使用Eclipse建立專案,詳情略。
- 下載InternetExplorerDriver或GeckoDriver,請放在本機的磁碟目錄,這是用來幫你開啟IE瀏覽器的驅動器。
- 主要代碼如下
- 點選執行
// Prepare IE driver
WebDriver driver = null;
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.ie.driver", "D:/IEDriverServer.exe");
driver = new InternetExplorerDriver(capabilities);
/* Prepare Firefox driver
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Sean\\Downloads\\geckodriver.exe");
driver = new FirefoxDriver(capabilities);
*/
// Automation
driver.get("${Your URL}");
driver.findElement(By.id("account")).sendKeys("${Your Account}");
driver.findElement(By.id("password")).sendKeys("${Your Password}");
driver.findElement(By.xpath("${Your xPath}"));
driver.click();