Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a PrimeFaces radio button with Selenium Webdriver

I am trying to write Selenium tests to select a radio button. Below is the html from 'view Source'.

<table id="surveyForm:surveyUrlType" class="ui-selectoneradio ui-widget" style="width:100%;margin-top:10px;margin-bottom: 10px;">
    <tbody>
    <tr>
        <td>
            <div class="ui-radiobutton ui-widget">
                <div class="ui-helper-hidden-accessible">
                    <input id="surveyForm:surveyUrlType:0" name="surveyForm:surveyUrlType" type="radio" value="TYPED" checked="checked" onchange="com.ssi.feasibility.surveyView.showSurveyType(this);">
                </div>
                <div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default ui-state-active">
                    <span class="ui-radiobutton-icon ui-icon ui-icon-bullet"></span>
                </div>
            </div>
        </td>
        <td><label for="surveyForm:surveyUrlType:0">Enter Survey URL</label></td>
        <td>
            <div class="ui-radiobutton ui-widget">
                <div class="ui-helper-hidden-accessible">
                    <input id="surveyForm:surveyUrlType:1" name="surveyForm:surveyUrlType" type="radio" value="FILE" onchange="com.ssi.feasibility.surveyView.showSurveyType(this);">
                </div>
                <div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default">
                    <span class="ui-radiobutton-icon"></span>
                </div>
            </div>
        </td>
        <td><label for="surveyForm:surveyUrlType:1">Upload Survey URLs</label></td>
    </tr>
    </tbody>
</table>

I want to select 'Upload Survey URLs' radio button.

I have tried several different approaches to select the radio button. Here are a few:

        $("#surveyForm\\surveyUrlType").click();

        This gives me the error : 
        $("#surveyForm\\:surveyUrlType\\:1").first().click()

Error- Element is not clickable at point (809, 367). Other element would receive the click: ...

Below give me NoSuchElementFound:

        driver.findElement(By.id("surveyForm:surveyUrlType:1")).click()
        driver.findElement(By.xpath("//input[@type='radio' and @id='surveyForm\\:surveyUrlType\\:1']")).click()
        driver.findElement(By.xpath("//input[@value='FILE']")).click()
        driver.findElement(By.cssSelector("#surveyForm\\:surveyUrlType\\:1")).click()

Below don't give me any error but they also do not select the radio button.

        $(".ui-radiobutton-box ui-widget ui-corner-all ui-state-default")[1].click()
        $(".ui-radiobutton-box ui-widget ui-corner-all ui-state-default").click()
        $("#surveyForm\\:surveyUrlType").find(".ui-radiobutton-box ui-widget ui-corner-all ui-state-default").find("span").click()

But none of these work. What am I missing?

like image 867
user1860447 Avatar asked Dec 17 '13 22:12

user1860447


People also ask

How do I select a radio button in Selenium WebDriver?

How to locate a radio button using the name locator? If the radio button contains a unique value in the "name " attribute, then we can use the Name locator of Selenium to locate the radio button. Once located, we can perform click operation on the radio button element to select the radio button.

Which method is used to select radio button in Selenium?

whereas using checkbox, we can select multiple options. Using Click() method in Selenium we can perform the action on the Radio button and on Checkbox. WebElement maleRadioBtn = driver.

Which method is used to select the radio button?

Radio Buttons too can be toggled on by using the click() method.

How do I select the second radio button in Selenium?

We can select a radio in a page in Selenium with the help of click() method. First of all we need to uniquely identify the checkbox with the help of any of the locators like css, xpath, id, class and so on. Next we have to use findElement() method to locate the element and finally perform the clicking action.


1 Answers

This is how I got it to work finally !!

 driver.findElement(By.cssSelector("label[for='surveyForm\\:surveyUrlType\\:1']")).click()
like image 188
user1860447 Avatar answered Sep 30 '22 14:09

user1860447