如何使用 Selenium 根據值查詢一個單選按鈕元素?
我們可以使用 Selenium webdriver 根據值查詢一個單選按鈕元素。html 文件中的單選按鈕有一個標記名稱 input,並且它的型別屬性設定為 radio。透過在識別出單選按鈕後使用 click() 方法,可以選擇該單選按鈕。
讓我們來看看單選按鈕的 html 程式碼。為了根據值屬性識別它,我們必須使用 xpath 或 css 定位器。
示例
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class RadioButnVal{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url="https://tutorialspoint.tw/selenium/selenium_automation_practice.htm"; driver.get(url); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify element with value WebElement l=driver.findElement(By.cssSelector("input[value='3']")); //select radio button with click() l.click(); driver.quit(); } }
輸出
廣告