設置驅動的方法(Chrome 親測ok)


驅動下載地址 http://selenium-release.storage.googleapis.com/index.html

package com.selenium.java.webdriver2testng;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Point;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class OpenDriverDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
String baseurl = "https://www.baidu.com";
// DesiredCapabilities類實現分布式指定執行自動化測試環境
DesiredCapabilities caps = DesiredCapabilities.chrome();
// 設置使用的瀏覽器名稱
caps.setBrowserName("chrome");
caps.setPlatform(Platform.WINDOWS);
System.setProperty("webdriver.chrome.driver",
"drivers/chromedriver.exe");
driver = new ChromeDriver();

// Firefox瀏覽器安裝位置最好默認,不然就需要指定瀏覽器路徑,瀏覽器調用不起來,的原因注意查看日志,從驅動版本和瀏覽器的版本兼容查起
// System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Tencent\\Mozilla Firefox\\firefox.exe");
// System.setProperty("webdriver.gecko.driver",
// "drivers/geckodriver64.exe");
// driver=new FirefoxDriver();

// 設置瀏覽器的屬性,key,value,對應里面的值,驅動路徑可以是相對路徑
// System.setProperty("webdriver.ie.driver",
// "drivers/IEDriverServer.exe");
// 創建一個瀏覽器驅動實例
// driver = new InternetExplorerDriver();
// 實例即對象,調用瀏覽器驅動封裝好的方法,get()打開網址,里面是一個字符串url,同時打開瀏覽器的方法還有navigate().to()
// driver.get("https://www.baidu.com");
driver.navigate().to(baseurl);
// 窗口最大化
driver.manage().window().maximize();
// 設置隱私等待時間,
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("獲取當前窗口的title:" + driver.getTitle());// 返回的是一個字符串類型

// 操作一下輸入框,點擊查詢selenium的內容
driver.findElement(By.id("kw")).sendKeys("selenium");
driver.findElement(By.id("su")).click();

// 用例執行完,關閉瀏覽器,這個是關閉當前,如果只有一個就是所有。quit()是關閉退出所有關聯的窗口
driver.close();
}
}
原文鏈接:https://blog.csdn.net/u011466469/article/details/88858093


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM