【发布时间】:2019-03-11 22:07:45
【问题描述】:
我正在尝试通过网站自动化 Selenium 中的 RadioButton 场景 - http://jqueryui.com/checkboxradio/
我尝试使用 CssSelector 的 xpath,自定义 xpath,但对我没有任何效果。我最终不得不使用绝对 xpath(虽然不是最佳实践)才能使代码正常工作。
public class CheckBoxAndRadioButtons {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver" , "C:/Users/User/Desktop/Selenium Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://jqueryui.com/checkboxradio/");
driver.manage().window().maximize();
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement radiobox = driver.findElement(By.xpath("/html/body/div/fieldset[1]/label[1]/span[1]"));
boolean isDisplayedstatus = radiobox.isDisplayed();
System.out.println(isDisplayedstatus);
boolean isEnabledstatus = radiobox.isEnabled();
System.out.println(isEnabledstatus);
boolean isSelectedstatus = radiobox.isSelected();
System.out.println(isSelectedstatus);
Thread.sleep(2000);
List<WebElement> ele = driver.findElements(By.xpath("//input[@type ='radio']"));
for(WebElement we:ele) {
if(!we.isSelected()) {
we.click();
}
}
}
}
【问题讨论】:
标签: java selenium xpath webdriver webdriverwait