How to check if a radio button is selected or not using 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>

this is html code

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

+4
source share
1 answer

In Java, this would do this:

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, text will be displayed. If you want to use text somewhere, I suggest that you put it in a line instead:

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

Hope this answers your question.

EDIT: The following is an update to other methods.

If className default_shipping_addressis unique (for example, it is not used anywhere on the page), you can try to find 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(driver.findElement(By.className("selectCard_left"))){
    System.out.println(driver.findElement(By.xpath("//div[@class='selectCard_left']/span[1]")).getText());
}

, xpath. , , xpath: http://www.w3schools.com/XPath/xpath_syntax.asp

, .

+5

Source: https://habr.com/ru/post/1538568/


All Articles