Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作

 WebElement selector = driver.findElement(By.id("Selector"));
Select select = new Select(selector);
select.selectByIndex(3);
select.selectByVisibleText("桃子");
select.selectByValue("apple"); System.out.println(select.getAllSelectedOptions().toString());
Thread.sleep(10000);
WebElement showSelectResult = driver.findElement(By.name("showSelectResult"));
showSelectResult.click();
Thread.sleep(10000);
Alert yourSelect = driver.switchTo().alert();
Assert.assertTrue(yourSelect.getText().contains("苹果"));
Thread.sleep(10000);
yourSelect.accept(); // for radio Box WebElement duRadioBox = driver.findElement(By.cssSelector("div#radio>input.Baidu"));
WebElement aLiRadioBox = driver.findElement(By.cssSelector("div#radio>input.Alibaba"));
WebElement TXRadioBox = driver.findElement(By.cssSelector("div#radio>input.Tencent"));
WebElement MiRadioBox = driver.findElement(By.cssSelector("div#radio>input.Mi"));
Thread.sleep(10000);
Assert.assertTrue(TXRadioBox.isSelected());
Assert.assertTrue(!MiRadioBox.isEnabled());
Thread.sleep(10000); try { if (duRadioBox.isEnabled()) {
duRadioBox.click();
System.out.println("元素被点击");
}
} catch (Exception e) { System.out.println("点击元素失败。" + e.toString());
} Assert.assertTrue(duRadioBox.isSelected());
System.out.println(duRadioBox.isSelected());
Thread.sleep(10000); aLiRadioBox.click();
Assert.assertTrue(aLiRadioBox.isSelected());
Thread.sleep(10000); // for checkBox List<WebElement> webCheckBox = driver.findElements(By.xpath("//input[@type='checkbox']")); for (WebElement e : webCheckBox) {
e.click();
Assert.assertTrue(e.isSelected());
Thread.sleep(10000);
}
上一篇:Docker 安装 MySQL


下一篇:数据库基础知识