Java 如何检查单选按钮是否在 Selenium WebDriver 中被选中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23800792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the radio button is selected or not in Selenium WebDriver?
提问by user3538483
Here is my HTML code
这是我的 HTML 代码
<div class="selectCard_left">
<input id="26110162" class="default_shipping_address" type="radio" name="address" checked="true">
<span>Seleccionar como tarjeta predeterminada</span>
I am trying with driver.findElement(By.id("17390233")).isSelected();
, but I am not getting any value.
我正在尝试使用driver.findElement(By.id("17390233")).isSelected();
,但我没有得到任何价值。
采纳答案by HemaSundar
driver.findElement(By.id("26110162")).isSelected();
or
或者
String str = driver.findElement(By.id("26110162")).getAttribute("checked");
if (str.equalsIgnoreCase("true"))
{
System.out.println("Checkbox selected");
}
if the ID is changing... use the following XPATH:
如果 ID 正在更改...使用以下 XPATH:
//input[span='Seleccionar como tarjeta predeterminada']
or
或者
//input[@name='address' and @type='radio']
回答by Vignesh
.isSelected() function will returns you a boolean value "True" or "False" ,depending on that you can check the condition and enable or leave the radio button selected. driver.findElement(By.cssSelector("input[id='26110162']")).isSelected().
.isSelected() 函数将返回一个布尔值 "True" 或 "False" ,具体取决于您可以检查条件并启用或保持选中单选按钮。driver.findElement(By.cssSelector("input[id='26110162']")).isSelected()。
Declare a boolean value and store the result then provide an if condiiton to validate
声明一个布尔值并存储结果,然后提供一个 if 条件来验证
回答by Deepika G
You can try any of this method:-
您可以尝试以下任何一种方法:-
selenium.isChecked(Locator);
List<WebElement> radio = driver.findElements(By.name("address")); radio.get(0).getAttribute("checked"));
selenium.isChecked(Locator);
List<WebElement> radio = driver.findElements(By.name("address")); radio.get(0).getAttribute("checked"));
Hope it will help...
希望它会有所帮助...
回答by Lucas Neves
Try in selenium java:
在硒 java 中尝试:
String name = driver.findElement(By.xpath("path"));
assertEquals(name.isSelected(),true);