How to check if radio button is selected or not using Selenium WebDriver? How to check if radio button is selected or not using Selenium WebDriver? selenium selenium

How to check if radio button is selected or not using Selenium WebDriver?


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.

EDIT: Here is an update of other ways to try.

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());}

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());}

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.