如何使用Selenium WebDriver检查单选按钮是否被选中? [英] How to check if radio button is selected or not using Selenium WebDriver?

查看:757
本文介绍了如何使用Selenium WebDriver检查单选按钮是否被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div class="my_account_module_content">
<h3 class="my_account_module_content_title">
Mi Dirección 1
<br>
<span>Predeterminada</span>

<div class="selectCard_left">
<input id="17390233" class="default_shipping_address" type="radio" name="address" checked="true">
<span>Seleccionar como tarjeta predeterminada</span>
</div>

这是HTML代码

如果选择的单选按钮为true,则打印班级跨度值? 请帮助我.

If radio button selected is true then print the class span value? please help me..

推荐答案

在Java中,这可以做到:

In Java, this would do it:

if(driver.findElement(By.id("17390233")).isSelected()){
    System.out.println(driver.findElement(By.xpath("//input[@id='17390233']/following-sibling::span[1]")).getText());
}

如果选择了单选按钮,则将显示文本.如果您想在某处使用文本,建议您将其放在字符串中:

If the radio button is selected, then the text will show. If you want to use the text somewhere, I suggest you put it in a string instead:

String spanText = driver.findElement(By.xpath("//input[@id='17390233']/following-sibling::span[1]")).getText();

希望这能回答您的问题.

Hope this answers your question.

这是其他尝试方法的更新.

Here is an update of other ways to try.

如果className default_shipping_address是唯一的(例如,在页面上的其他任何地方都未使用过),则可以尝试通过 className 定位元素:

If the className default_shipping_address is unique (e.g. not used anywhere else on the page), you may try locating the element by className:

if(driver.findElement(By.className("default_shipping_address")).isSelected()){
    System.out.println(driver.findElement(By.xpath("//input[@class='default_shipping_address']/following-sibling::span[1]")).getText());
}

如果该类不是唯一的,那么DIV的className selectCard_left是吗?

If that class is not unique, maybe the DIV's className selectCard_left is?

if(driver.findElement(By.className("selectCard_left"))){
    System.out.println(driver.findElement(By.xpath("//div[@class='selectCard_left']/span[1]")).getText());
}

如果所有className都不唯一,则需要完整的xpath表达式.如果您仍然无法获得该文本,请参考阅读有关如何使用xpath的内容: http://www.w3schools.com/XPath/xpath_syntax.asp

If none of the classNames are unique, a complete xpath expression is required. If you still are unable to get that text, I refer to reading up on how to use xpath: http://www.w3schools.com/XPath/xpath_syntax.asp

我希望您发现此信息有用.

I hope that you find this information useful.

这篇关于如何使用Selenium WebDriver检查单选按钮是否被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆