Selenium java 環境搭建

來源:互聯網
上載者:User

標籤:準備就緒   開啟   public   set   code   搜尋方塊   .com   入門   pos   

Selenium2(webdirver)入門之環境搭建(Java版)

一、開發環境:

  1、JDK1.6

  2、Eclipse:Version: Kepler Service Release 1,:http://www.eclipse.org/downloads/

  3、Selenium:selenium-java-2.52.0.zip,:http://docs.seleniumhq.org/download/

解壓selenium-java包,這個包裡麵包含四部分,如:

  

 

二、建立一個Java Project:

  1、然後把上面解壓出來的檔案拷到建立的project目錄下,目錄結構如:

  

  2、添加build path,項目目錄右鍵-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs

  把libs檔案夾下的jar包全部添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcs

  

  3、添加完之後目錄結構如,多了Referenced Libraries,這裡就是上面那一步添加進去的jar包:

  

  4、關聯webdriver的源碼:

  

至此,環境工作準備就緒,下面來寫一個簡單的小例子。

 

 三、在src下面建立測試類別,如:

   

代碼如下,主要是開啟百度,然後在搜尋方塊輸入glen,點擊搜尋按鈕,關閉瀏覽器。

 1 package com.selenium.Glen; 2  3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.firefox.*; 7  8 public class TestHelloWorld { 9 10     public static void main(String[] args) {11         12         //如果Firefox瀏覽器沒有預設安裝在C盤,需要制定其路徑13         //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 14         WebDriver driver = new FirefoxDriver();15         driver.get("http://www.baidu.com/");16         17         driver.manage().window().maximize();18         19         WebElement txtbox = driver.findElement(By.name("wd"));20         txtbox.sendKeys("Glen");21         22         WebElement btn = driver.findElement(By.id("su"));23         btn.click();24         25         driver.close();26 27     }28 29 }

 然後直接右鍵-->Run As-->Java Application就可以看到效果了

Selenium java 環境搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.