<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Selenium Form WebElement:文本框,提交按鈕,sendkeys(),click() > 原文: [https://www.guru99.com/accessing-forms-in-webdriver.html](https://www.guru99.com/accessing-forms-in-webdriver.html) 表單是從網站訪問者那里接收信息的基本網絡元素。 Web 表單具有不同的 GUI 元素,例如文本框,密碼字段,復選框,單選按鈕,下拉列表,文件輸入等。 我們將看到如何使用帶有 Java 的 Selenium Web Driver 訪問這些不同的表單元素。 **Selenium 將每個表單元素封裝為 WebElement 的對象。** 它提供??API,以查找元素并對其進行操作,例如在文本框中輸入文本,單擊按鈕等。我們將看到可用于訪問每個表單元素的方法。 在本教程中,我們將看到如何識別以下表單元素 * [WebElement 簡介,findElement(),findElements()](#1) * [輸入框](#2) * [在輸入框中輸入值](#3) * [刪除輸入框中的值](#4) * [按鈕](#5) * [提交按鈕](#6) * [完整代碼](#9) * [故障排除](#10) ## WebElement 簡介,findElement(),findElements() Selenium Web 驅動程序將一個簡單的表單元素封裝為 **WebElement 的對象。** WebDriver 通過多種技術基于 Web 元素的不同屬性來識別表單元素,例如 ID,Name,Class,XPath,Tagname,CSS 選擇器,鏈接文本等。 Web 驅動程序提供以下兩種方法來查找元素。 * **findElement()** –查找單個 Web 元素并作為 WebElement 對象返回。 * **findElements()** –返回與定位器條件匹配的 WebElement 對象的列表。 讓我們看一下獲取單個元素的代碼片段-使用 findElement()方法將網頁中的文本字段作為 WebElement 的對象。 在后續教程中,我們將介紹用于查找多個元素的 findElements()方法。 **步驟 1:**我們需要導入此包才能創建 Web Elements 的對象 ![](https://img.kancloud.cn/09/37/093769c5970c8a892e6cac9ad278079e_286x38.png) **步驟 2:**我們需要調用 WebDriver 類上可用的 findElement()方法并獲取 WebElement 對象。 請參閱下面的內容以了解其操作方法。 ## 輸入框 輸入框引用以下兩種類型之一: 1. **文本字段**-接受輸入值并按原樣顯示它們的文本框。 2. **Password Fields**- text boxes that accept typed values but mask them as a series of special characters (commonly dots and asterisks) to avoid sensitive values to be displayed. ![](https://img.kancloud.cn/ce/ce/cece0eddd464711c959b16550c77532c_395x154.png) ### **定位器** findElement()方法采用一個參數,該參數是元素的定位符。 不同的定位器(例如 By.id(),By.name(),By.xpath(),By.CSSSelector()等)使用其屬性(例如 ID,名稱或路徑, 等等 您可以使用諸如 Fire path 之類的插件來獲取有關獲取元素 ID,xpath 等的幫助。 使用下面給出的示例站點 [http://demo.guru99.com/test/login.html](http://demo.guru99.com/test/login.html) ,該代碼用于使用 ID 定位符和“密碼”字段來使用 ID 定位符來定位“電子郵件地址”文本字段 名稱定位器。 ![](https://img.kancloud.cn/d4/63/d4632e5b593dca2e7bf0dee63ff39be1_743x180.png) 1. 電子郵件文本字段按 ID 定位 2. 密碼字段按名稱定位 ## 在輸入框中輸入值 要將文本輸入到“文本字段”和“密碼字段”,sendKeys()是 WebElement 上可用的方法。 使用 [http://demo.guru99.com/test/login.html](http://demo.guru99.com/test/login.html) 網站的相同示例,這是我們查找“文本”字段和“密碼”字段并在其中輸入值的方法。 ![](https://img.kancloud.cn/f6/ee/f6eef01ab98560ad5e2e64969cf9b2be_780x428.png) 1. 使用 id 定位器找到“電子郵件地址”文本字段。 2. 使用名稱定位器找到“密碼”字段 3. 使用 sendKeys()方法將文本輸入“電子郵件地址”。 4. 使用 sendKeys()方法在“密碼”字段中輸入密碼。 ## 刪除輸入框中的值 **clear()**方法用于刪除輸入框中的文本。 **此方法不需要參數**。 下面的代碼段將清除“電子郵件”或“密碼”字段中的文本 ![](https://img.kancloud.cn/e3/5e/e35ebacd4afb75cedf22ef65559fa47c_663x268.png) ## 按鈕 可以使用 click()方法訪問按鈕。 在上面的例子中 1. 查找登錄按鈕 2. 單擊站點登錄頁面中的“登錄”按鈕以登錄到站點。 ![](https://img.kancloud.cn/e1/56/e156efec57eb2ee7c6b4b9846ef6528f_745x293.png) ## 提交按鈕 提交按鈕用于將整個表單提交到服務器。 如上所述,我們可以像常規按鈕一樣在 Web 元素上使用 click()方法,也可以在表單中的任何 Web 元素上或在 Submit 按鈕本身上使用 Submit()方法。 ![](https://img.kancloud.cn/d8/8b/d88b602b86fefab491c20c1eb6910563_766x360.png) **使用 Submit()時,WebDriver 將查找 DOM 以了解該元素屬于哪個表單,然后觸發其 Submit 函數。** ## 完整代碼 這是完整的工作代碼 ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.*; public class Form { public static void main(String[] args) { // declaration and instantiation of objects/variables System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String baseUrl = "http://demo.guru99.com/test/login.html"; driver.get(baseUrl); // Get the WebElement corresponding to the Email Address(TextField) WebElement email = driver.findElement(By.id("email")); // Get the WebElement corresponding to the Password Field WebElement password = driver.findElement(By.name("passwd")); email.sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it. "); password.sendKeys("abcdefghlkjl"); System.out.println("Text Field Set"); // Deleting values in the text box email.clear(); password.clear(); System.out.println("Text Field Cleared"); // Find the submit button WebElement login = driver.findElement(By.id("SubmitLogin")); // Using click method to submit form email.sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it."); password.sendKeys("abcdefghlkjl"); login.click(); System.out.println("Login Done with Click"); //using submit method to submit the form. Submit used on password field driver.get(baseUrl); driver.findElement(By.id("email")).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it."); driver.findElement(By.name("passwd")).sendKeys("abcdefghlkjl"); driver.findElement(By.id("SubmitLogin")).submit(); System.out.println("Login Done with Submit"); //driver.close(); } } ``` ## 故障排除 如果在查找元素時遇到 NoSuchElementException(),則意味著在 Web 驅動程序訪問頁面時在頁面中找不到該元素。 1. 在 Chrome 中使用 Firepath 或 Inspect Element 再次檢查定位器。 2. 現在檢查您在代碼中使用的值是否與 Firepath 中的元素的值不同。 3. 有些屬性對于少數元素是動態的。 如果發現該值不同且正在動態變化,請考慮使用 By.xpath()或 By.cssSelector(),它們更可靠,但方式更復雜。 4. 有時,這也可能是一個等待問題,即 Web 驅動程序甚至在頁面完全加載之前就執行了代碼等。 5. 使用隱式或顯式等待在 findElement()之前添加一個等待。 ### 摘要 * 下表總結了訪問上述每種元素的命令 | **元素** | **命令** | **說明** | | --- | --- | --- | | **輸入框** | sendKeys() | 用于在文本框中輸入值 | | 明確() | 用于清除文本框的當前值 | | **鏈接** | click() | 用于單擊鏈接并等待頁面加載完成,然后再繼續執行下一個命令。 | | **提交按鈕** | 提交() | | * WebDriver 允許在多個 SELECT 元素中選擇多個選項。 * 您可以在表單中的任何元素上使用 commit()方法。 WebDriver 將自動觸發該元素所屬表單的提交功能。
                  <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>

                              哎呀哎呀视频在线观看