public class radioExamJavaScr { public static void main(String[] args) throws IOException { WebDriver driver = new FirefoxDriver(); EventFiringWebDriver dr = new EventFiringWebDriver(driver); dr.get("http://www.makemytrip.com/"); dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); ((JavascriptExecutor)dr).executeScript("document.getElementById('roundtrip_r').click();"); WebElement one_way = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('oneway_r') ;"); System.out.println(one_way.isSelected()); WebElement round_trip = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('roundtrip_r') ;"); System.out.println(round_trip.isSelected()); } }
In the above example, I select the radio button with "ROUND TRIP" using "JavaScript".
The last four lines should check and see if the expected switch on the page is selected or not.
NOTE. I give a simple solution to solve the problem (radio selection) on the selected web page. You can write better code. (the user can write a method to receive the radio ID and skip the entire existing radio button to see which one is selected).
Mkod
source share