Selenium_Chrome瀏覽器調用
阿新 • • 發佈:2017-09-24
ora test pre bsp key cli netd 下載地址 before
備註:由於本人使用的是Selenium 2.44的版本,所以支持的Chrome的版本為Chrome v29-32,所以下面提供的Chrome下載地址也Chrome30的地址。
1)使用Selenium調用Chrome瀏覽器前期準備:
1、下載Chrome瀏覽器,下載地址參考:https://pan.baidu.com/share/link?shareid=305671&uk=3355546973#list/path=%2FChrome
2、下載Chrome瀏覽器的WebDriver,下載地址參考:http://chromedriver.storage.googleapis.com/index.html
2)編寫Selenium代碼
備註:chromedriver.exe可以放在本地硬盤的任意位置,我是放在D:\\BaiduNetdiskDownload\\Chrome下面的,所以下面代碼中加載的路徑也是D:\\BaiduNetdiskDownload\\Chrome這個路徑。
package com.testng.webdriver; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class TestChrome { public WebDriver driver; String baseUrl = "http://www.sogou.com/"; @Test public void testSearch() { driver.get(baseUrl); driver.findElement(By.id("query")).sendKeys("光榮之路自動化測試"); driver.findElement(By.id("stb")).click(); } @BeforeMethod public void beforeMethod() { //設置谷歌瀏覽器默認存儲位置 System.setProperty("webdriver.chrome.driver", "D:\\BaiduNetdiskDownload\\Chrome\\chromedriver.exe"); driver = new ChromeDriver(); //設置瀏覽器為全屏模式 driver.manage().window().maximize(); } @AfterMethod public void afterMethod() { //退出瀏覽器 driver.quit(); } }
至此Chrome瀏覽器調用OK
Selenium_Chrome瀏覽器調用