mvillegas
mvillegas

Reputation: 57

How to select and click on a radio button in selenium web driver with java

I have written a simple script of selenium web driver in java to select and click on a Radio button , but I am unable to select and click on the second radio button 'ONE WAY FLIGHT' on web page: http://www.lot.com/pl/en

Here is my code:

WebDriver driver = new FirefoxDriver();
String web = "http://www.lot.com/pl/en";
driver.get(web);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement element;

element = driver.findElement(By.id("select2-departureAirport-container"));
element.click();
element.sendKeys("WAW");
element.sendKeys(Keys.ENTER);

/* RADIO BUTTON ERROR */
element = driver.findElement(By.cssSelector("input[value='SINGLE']"));
elementRadio.click();

And this is the error:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException:

And this is the fragment of HTML on the web page http://www.lot.com/pl/en:

<div class="b-row">
                <div class="b-column twelve">
                    <fieldset class="flight-type" role="radiogroup" aria-required="true">
                        <legend class="acc-hide">Choose flight type</legend>
                        <label class="booker-label radio-label">
                            <input data-f-focus="radio" id="ticketTypeReturn" type="radio" name="ticketType" class="required" value="RETURN" checked="checked" />

                            <span class="ci" aria-hidden="true"></span>
                            <span class="ci-label">Round-trip flight</span>
                        </label>
                        <label class="booker-label radio-label g-no-margin">

                            <input data-f-focus="radio" type="radio" name="ticketType" class="required" value="SINGLE" />
                            <span class="ci cis" aria-hidden="true"></span>
                            <span class="ci-label">One-way flight</span>
                        </label>
                        <p id="ticketTypeReturn--required" class="b-v-error" role="alert">Choose flight type</p>
                    </fieldset>

                </div>
            </div>

I am unable to understand why it is throwing exception.

Upvotes: 0

Views: 5327

Answers (2)

Kapil
Kapil

Reputation: 407

Did you try with xpath?

elementRadio = driver.findElement(By.xpath("//*[@id="flightBookingForm"]/div[2]/div[1]/div/fieldset/label[2]/span[2]"));
elementRadio.click();

Upvotes: 0

Shubham Jain
Shubham Jain

Reputation: 17553

Use below code :-

WebDriver driver = new FirefoxDriver();
String web = "http://www.lot.com/pl/en";
driver.get(web);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement element;

element = driver.findElement(By.id("select2-departureAirport-container"));
element.click();
element.sendKeys("WAW");
element.sendKeys(Keys.ENTER);

/* RADIO BUTTON ERROR */
element = driver.findElement(By.cssSelector("input[value='SINGLE']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Upvotes: 2

Related Questions