在本節(jié)中,我們將了解如何使用下拉框進(jìn)行交互。我們可以用“selectByVisibleText'或'selectByIndex'或'selectByValue'的方法選擇一個(gè)選項(xiàng)。
讓我們明白,如何使用交互復(fù)選框 - http://www.calculator.net/interest-calculator.htmll。我們還可以檢查下拉框中選擇/啟用/可見。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class webdriverdemo
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.navigate().to("http://www.calculator.net/interest-calculator.htmll");
driver.manage().window().maximize();
// Selecting an item from Drop Down list Box
Select dropdown = new Select(driver.findElement(By.id("ccompound")));
dropdown.selectByVisibleText("continuously");
// you can also use dropdown.selectByIndex(1) to select second element as index starts with 0.
// You can also use dropdown.selectByValue("annually");
System.out.println("The Output of the IsSelected " + driver.findElement(By.id("ccompound")).isSelected());
System.out.println("The Output of the IsEnabled " + driver.findElement(By.id("ccompound")).isEnabled());
System.out.println("The Output of the IsDisplayed " + driver.findElement(By.id("ccompound")).isDisplayed());
driver.close();
}
}
在執(zhí)行時(shí),向下設(shè)置與指定的值與命令的輸出下拉顯示在控制臺(tái)中。