<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Selenium Java 教程 > 原文: [https://javatutorial.net/selenium-java-tutorial](https://javatutorial.net/selenium-java-tutorial) 本教程將說明如何使用 Java 運行 Selenium WebDriver [Selenium](http://www.seleniumhq.org/ "Selenium") 是用于測試 Web 應用程序的強大框架。 使用 Selenium,您可以自動瀏覽,單擊和提交網頁上的表單。 對網絡應用程序進行更改后,最好通過一些手動和自動測試來運行它并驗證一切是否正常運行。 本教程將向您展示如何使用 Java 編程語言編寫測試腳本。 我假設您已經對 Java 有一定的經驗。 如果沒有閱讀,請先閱讀我們的 [Java 初學者教程](http://javatutorial.net/category/java-basics "Java Beginner Tutorials")。 ## Selenium Maven 構建 如果您使用 [Maven](https://javatutorial.net/how-to-install-maven-on-windows-linux-and-mac) 來構建項目,請在`.pom`文件中使用以下依賴項 ```java <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.44.0</version> </dependency> ``` ## `Selenium.jar`文件 如果您喜歡老式的方法,則必須從 Selenium 網站下載所需的`.jar`。 1. 轉到[ Selenium 下載頁面](http://www.seleniumhq.org/download/) 2. 下載 Java 2.xx zip 文件 ![selenium-download](https://img.kancloud.cn/0b/7d/0b7d3939de8739e48c80b577a6bcaca8_697x260.jpg) 3. 將`selenium-java-2.44.0.jar`和所有 jar 從`libs`文件夾復制到您的項目中 ## Selenium 控制臺示例 這是一個基本的 Selenium Java 示例。 它使用默認的`HtmlUnitDriver`以類似控制臺的樣式提取頁面標題。 ```java import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class SeleniumConsoleExample { public static void main(String[] args) { // Create HTML Unit Driver - this is the build in Selenium client WebDriver driver = new HtmlUnitDriver(); // go to url driver.get("http://javatutorial.net"); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } } ``` ## Selenium Firefox 示例 在許多情況下,您將需要 Selenium 與動態創建的元素一起使用。 為此,您將需要一個類似 Firefox 或 Google Chrome 的瀏覽器窗口。 以下示例需要在默認位置安裝 Firefox Web 瀏覽器。 1. Selenium 將打開一個單獨的 Firefox 窗口并轉到 https://javatutorial.net 2. 查看此頁面頂部的搜索按鈕(放大鏡),是 - 當前正在閱讀的頁面頂部 ?? Selenium 將光標移至該位置以顯示搜索字段 3. 它將輸入搜索詞“java”并提交表格 4. 等待 5 秒鐘,然后關閉瀏覽器窗口 ```java import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class SeleniumFirefoxExample { public static void main(String[] args) throws Exception { // create a Firefox Web Driver WebDriver driver = new FirefoxDriver(); // open the browser and go to JavaTutorial Network Website driver.get("https://javatutorial.net"); // find the search button on the page WebElement searchButton = driver.findElement(By .className("search-submit")); // create an action handler Actions actions = new Actions(driver); // use the action handler to move the cursor to given element actions.moveToElement(searchButton).perform(); // wait until the search field is presented on the webpage and create an // element WebElement searchField = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.name("s"))); // puts the text "java" into the search field searchField.sendKeys("java"); // submit the search (submit the form) searchField.submit(); // wait 5 seconds and close the browser Thread.sleep(5000); driver.quit(); } } ``` ## Selenium Chrome 示例 要使 Selenium 使用 Google Chrome 瀏覽器,您需要下載并運行獨立的 Chrome WebDriver。 1.下載適用于您操作系統的 [Chrome Web 驅動程序](http://chromedriver.storage.googleapis.com/index.html?path=2.13/),該歸檔文件包含一個可執行文件 2.啟動可執行文件 – 它將在端口 9515 上運行本地服務器 3.在您的代碼中像這樣創建 WebDriver: ```java URL local = new URL("http://localhost:9515"); WebDriver driver = new RemoteWebDriver(local, DesiredCapabilities.chrome()); ``` 這是與上述使用 Chrome 網絡驅動程序相同的示例: ```java import java.net.URL; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class SeleniumChromeExample { public static void main(String[] args) throws Exception { // create a Chrome Web Driver URL local = new URL("http://localhost:9515"); WebDriver driver = new RemoteWebDriver(local, DesiredCapabilities.chrome()); // open the browser and go to JavaTutorial Network Website driver.get("http://javatutorial.net"); // find the search button on the page WebElement searchButton = driver.findElement(By .className("search-submit")); // create an action handler Actions actions = new Actions(driver); // use the action handler to move the cursor to given element actions.moveToElement(searchButton).perform(); // wait until the search field is presented on the webpage and create an // element WebElement searchField = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.name("s"))); // puts the text "java" into the search field searchField.sendKeys("java"); // submit the search (submit the form) searchField.submit(); // wait 5 seconds and close the browser Thread.sleep(5000); driver.quit(); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看