<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 WebDriver 中選擇復選框和單選按鈕 > 原文: [https://www.guru99.com/checkbox-and-radio-button-webdriver.html](https://www.guru99.com/checkbox-and-radio-button-webdriver.html) 在本教程中,我們將看到如何識別以下表單元素 * [單選按鈕](#7) * [復選框](#8) ## 單選按鈕 也可以使用 click()方法打開單選按鈕。 使用 [http://demo.guru99.com/test/radio.html](http://demo.guru99.com/test/radio.html) 進行練習,可以看到 radio1.click()切換了“ Option1”單選按鈕。 radio2.click()切換“ Option2”單選按鈕,而未選中“ Option1”。 ![](https://img.kancloud.cn/c9/20/c92006af146154d2789ee39c356a5953_754x288.png) ## 復選框 也可以使用 **click()**方法來打開/關閉復選框。 下面的代碼將兩次單擊 Facebook 的“保持登錄狀態”復選框,然后在打開時將結果輸出為 TRUE,在關閉時將結果顯示為 FALSE。 ![](https://img.kancloud.cn/8a/cc/8acc1cb559e560f93ddef0e97bd08bd5_604x344.png) ![](https://img.kancloud.cn/ae/74/ae7415e5e77c94ab72e8f88eb3241827_349x215.png) isSelected()方法用于知道復選框是打開還是關閉。 這是另一個示例: [http://demo.guru99.com/test/radio.html](http://demo.guru99.com/test/radio.html) ![](https://img.kancloud.cn/74/76/74763c2b99d05296723eec0ffbde7027_728x420.png) ## 完整的代碼 這是完整的工作代碼 ``` 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(); driver.get("http://demo.guru99.com/test/radio.html"); WebElement radio1 = driver.findElement(By.id("vfb-7-1")); WebElement radio2 = driver.findElement(By.id("vfb-7-2")); //Radio Button1 is selected radio1.click(); System.out.println("Radio Button Option 1 Selected"); //Radio Button1 is de-selected and Radio Button2 is selected radio2.click(); System.out.println("Radio Button Option 2 Selected"); // Selecting CheckBox WebElement option1 = driver.findElement(By.id("vfb-6-0")); // This will Toggle the Check box option1.click(); // Check whether the Check box is toggled on if (option1.isSelected()) { System.out.println("Checkbox is Toggled On"); } else { System.out.println("Checkbox is Toggled Off"); } //Selecting Checkbox and using isSelected Method driver.get("http://demo.guru99.com/test/facebook.html"); WebElement chkFBPersist = driver.findElement(By.id("persist_box")); for (int i=0; i<2; i++) { chkFBPersist.click (); System.out.println("Facebook Persists Checkbox Status is - "+chkFBPersist.isSelected()); } //driver.close(); } } ``` ## 故障排除 如果在查找元素時遇到 NoSuchElementException(),則意味著在 Web 驅動程序訪問頁面時在頁面中找不到該元素。 1. 在 Chrome 中使用 Firepath 或 Inspect Element 再次檢查定位器。 2. 現在檢查您在代碼中使用的值是否與 Firepath 中的元素的值不同。 3. 有些屬性對于少數元素是動態的。 如果發現該值不同且正在動態變化,請考慮使用 By.xpath()或 By.cssSelector(),它們更可靠,但方式更復雜。 4. 有時,這也可能是一個等待問題,即 Web 驅動程序甚至在頁面完全加載之前就執行了代碼等。 5. 使用隱式或顯式等待在 findElement()之前添加一個等待。 ### 摘要 * 下表總結了訪問上述每種元素的命令 | **元素** | **命令** | **說明** | | --- | --- | --- | | **復選框,** **單選按鈕** | click() | 用于打開/關閉元素 |
                  <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>

                              哎呀哎呀视频在线观看