简体   繁体   English

如何检查单选按钮是否在 Selenium WebDriver 中被选中?

[英]How to check if the radio button is selected or not in Selenium WebDriver?

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();我正在尝试使用driver.findElement(By.id("17390233")).isSelected(); , but I am not getting any value. ,但我没有得到任何价值。

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']

.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. .isSelected()函数将返回一个布尔值TrueFalse ,具体取决于您可以检查条件并启用或保持选中单选按钮。

driver.findElement(By.cssSelector("input[id='26110162']")).isSelected().

Declare a boolean value and store the result then provide an if condition to validate声明一个布尔值并存储结果,然后提供一个 if 条件来验证

You can try any of this method:-您可以尝试以下任何一种方法:-

  1. selenium.isChecked(Locator);

  2. List<WebElement> radio = driver.findElements(By.name("address")); radio.get(0).getAttribute("checked"));

Hope it will help...希望它会有所帮助...

Try in selenium java: 尝试selenium java:

String name = driver.findElement(By.xpath("path"));

assertEquals(name.isSelected(),true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver检查单选按钮? - How to check a radio button with Selenium WebDriver? 在<label>使用Selenium WebDriver</label>的列表中找到选定的单选按钮 - Find the selected radio button in a list of <label> using selenium webdriver 如何在Selenium Webdriver中选择单选按钮? 动态选择单选按钮 - How to select radio button in selenium webdriver ? dynamic select of radio button 如何使用Selenium WebDriver检查按钮是否可单击 - How to check if the button is clickable using selenium webdriver 如何检查单击按钮时是否选择了单选按钮? - How to check if radio buttons are selected on button click? 如何检查每个无线电组中的按钮是否被选中? - How to check is the button is selected in each radio group? 如何使用 Selenium WebDriver 读取 ::after 单选按钮元素的属性 - How to read ::after attributes of a radio button element using Selenium WebDriver 如何使用 java 在 selenium webdriver 中使用 select 单选按钮和复选框? - how to select radio button & checkbox in selenium webdriver with java? 如何使用 selenium webdriver 在 JQGrid 中选择单选按钮 - How to select Radio button in JQGrid using selenium webdriver 如何使用带有Java的Selenium WebDriver选择单选按钮? - How to select radio button using Selenium WebDriver with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM