13

I have a code which clicks on a radio button, at first I was using Chrome. Using the code below:

driver.findElement(By.id("radioButton1"))).click();

I got the error:

"org.openqa.selenium.WebDriverException: Element is not clickable at point (411, 675). Other element would receive the click: ..."

Doing research, I changed the code to:

actions.moveToElement(driver.findElement(By.id("radioButton1"))).click().perform();

Now, I am trying to use Internet Explorer driver. But it does not perform the click.

I tried the following:

driver.findElement(By.id("radioButton1")).sendKeys(Keys.ENTER);

actions.moveToElement(driver.findElement(By.id("radioButton1"))).click().perform();

((JavascriptExecutor) driver).executeScript("arguments[0].click()", driver.findElement(By.id("radioButton1")));

But none works. The first one just focuses on the button, so I added another sendKeys, but it doesn't work. The 2nd and 3rd, nothing happens.

Edit:

Adding HTML snippet.

<input name="btn1" class="w-rdo-native" id="radioButton1" type="radio" value="value1" bh="RDOINP" isrefresh="false">
<label class="w-rdo w-rdo-dsize" bh="RDO"></label>

And when I click on the radio button, the label gets an additional property upon click.

<label class="w-rdo w-rdo-dsize" bh="RDO" AWMouseDown="true"></label>

Additional edit:

The set of buttons look like this:

enter image description here

and as stated before, one button + label block has the following HTML structure:

<tr>
   <td>
      <div class="w-rdo-container">
          <input name="radioButtons" class="w-rdo-native" id="button1" type="radio" value="button1" bh="RDOINP" isrefresh="false">
          <label class="w-rdo w-rdo-dsize" bh="RDO">
          </label>
      </div>
  </td>
  <td class="sectionHead">Option 2
  </td>
</tr>

Upon clicking a button, the corresponding label gets an additional attribute:

<label class="w-rdo w-rdo-dsize" bh="RDO" AWMouseDown="true"></label>

It seems AWMouseDown seems to be the trigger to 'officially' click the button.

Edit :

Full HTML snippet of table. (Please note that this table has been cleansed so apologies for some mistake if I committed one.)

<table border="0" cellpadding="0" cellspacing="0" class="a-cptp-tbl">
    <tbody>
        <tr>
            <td>
                <div class="w-rdo-container">
                    <input checked class="w-rdo-native" id="btn1" name="radioBtn" type="radio" value="btn1"><label class="w-rdo w-rdo-dsize"></label>
                </div>
            </td>
            <td class="sectionHead">Option 1</td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <div class="w-rdo-container">
                    <input class="w-rdo-native" id="btn2" name="radioBtn" type="radio" value="btn2"><label class="w-rdo w-rdo-dsize"></label>
                </div>
            </td>
            <td class="sectionHead">Option 2</td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <div class="w-rdo-container">
                    <input class="w-rdo-native" id="btn3" name="radioBtn" type="radio" value="btn3"><label class="w-rdo w-rdo-dsize"></label>
                </div>
            </td>
            <td class="sectionHead">Option 3</td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <div class="w-rdo-container">
                    <input class="w-rdo-native" id="btn4" name="radioBtn" type="radio" value="btn4"><label class="w-rdo w-rdo-dsize"></label>
                </div>
            </td>
            <td class="sectionHead">Option 4</td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <div class="w-rdo-container">
                    <input class="w-rdo-native" id="btn5" name="radioBtn" type="radio" value="btn5"><label class="w-rdo w-rdo-dsize"></label>
                </div>
            </td>
            <td class="sectionHead">Option 5</td>
        </tr>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <div class="w-rdo-container">
                    <input class="w-rdo-native" id="btn6" name="radioBtn" type="radio" value="btn6"><label class="w-rdo w-rdo-dsize"></label>
                </div>
            </td>
            <td class="sectionHead">Option 6</td>
        </tr>
        <tr>
            <td></td>
        </tr>
    </tbody>
</table>
Kawamoto Takeshi
  • 596
  • 1
  • 3
  • 24

8 Answers8

10

Try using JavaScript like below:

WebElement radioBtn1 = driver.findElement(By.id("radioButton1"));
((JavascriptExecutor) driver).executeScript("arguments[0].checked = true;", radioBtn1);

If you are using QMetry Automation Framework, you should create custom radio button component like where you can override click method with such custom implementation.

user861594
  • 5,733
  • 3
  • 29
  • 45
3

Use ExplicitWait to wait for element until clickable and then have to click on that element

     WebElement element = driver.findElement(By.id("radioButton1"));
     WebDriverWait wait = new WebDriverWait(driver, 120);
     wait.until(ExpectedConditions.elementToBeClickable(element));

     element.click();

EDITED

If it is causing problem in IE browser. The cause is preventing to find element in IE browser is ActiveX Controls

So just you need to follow these steps -

  1. Go to Internet options > Advanced > security and do check below mentioned checks - enter image description here

  2. after check > apply and then don't forgot to restart your PC

Now simply run your script and try to click on that element using id

driver.findElement(By.id("button1")).click(); 

Hope this will work. Let us know if still face the same issue.

NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • Hi, thanks. It doesn't work. The explicit wait always timed out. – Kawamoto Takeshi Jan 09 '17 at 06:12
  • Might be there are many cause as you mentioned error `Element is not clickable` . follow this post http://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error – NarendraR Jan 09 '17 at 06:21
0

Can you try identifying the radio buttons using a list and then clicking on an element in the list using its index with get()?

List<WebElement> radioGrp = driver.findElements(By.name("xxxxxxxx"));
radioGrp.get(0).click();
Anish Pillai
  • 1,023
  • 7
  • 8
0

Not sure what is causing the problem.It works for me thought:

 public static IWebDriver driver;
    [Test]
    public void TestMethod1()
    {
        driver = new PhantomJSDriver();
        driver.Navigate().GoToUrl("file:///C:/Users/utripra/Desktop/test.html");
        driver.FindElement(By.Id("radioButton1")).Click();
ratr
  • 606
  • 1
  • 9
  • 24
0

It seems that the radio button is combination of the <input> and <label> tags, i.e. the <div> with class="w-rdo-container" or its <td> parent. I think so because the rapper <td> and the <td> where the label Option 2 is are siblings.

class="w-rdo-container" doesn't seem to be unique, so you can use xpath to go up the html tree from id="button1"

driver.findElement(By.xpath("//div[input[@id='button1']]")).click(); // to click the div
// or
driver.findElement(By.xpath("//td[div[input[@id='button1']]]")).click(); // to click the td
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Hi, thanks. I tried both and it didn't seem to work. I am not sure if xpath is applicable for IEDriver? – Kawamoto Takeshi Jan 11 '17 at 07:10
  • @KawamotoTakeshi Did you get any errors? or nothing happened? – Guy Jan 11 '17 at 07:13
  • @KawamotoTakeshi You could also try locating those elements some other way, I can base my answer only on the html snippets you provided. – Guy Jan 11 '17 at 07:14
  • Hi Guy, thanks. I tried seeing how it reacts again in Chrome and weirdly enough, you may be in the right track. Clicking on the corresponding TD still triggers the 'click'. I'll try to see how can I get the TD without an id. – Kawamoto Takeshi Jan 11 '17 at 07:22
  • Hi, I have changed it to CSS selector and nothing happens. Code proceeds like it clicked the td/div but nothing happens. No errors/stack trace in Eclipse. – Kawamoto Takeshi Jan 11 '17 at 07:55
  • @KawamotoTakeshi Are you using up to date IE driver and browser? if not try to update them. – Guy Jan 11 '17 at 07:59
  • Hi, I believe I have the latest driver which is 3.0.0. IE browser is up-to-date, which is 11. – Kawamoto Takeshi Jan 11 '17 at 08:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132878/discussion-between-guy-and-kawamoto-takeshi). @KawamotoTakeshi – Guy Jan 11 '17 at 08:09
0

Try following for clicking on Option 2 radio button:

driver.findElement(By.xpath("//td[normalize-space(text())='Option 2']/preceding::input[1]")).click();
Mahipal
  • 900
  • 1
  • 5
  • 7
0

Write a method that will accept the position of the radio button and click on the button by using cssSelector as follows:

driver.findElement(By.cssSelector("table.a-cptp-tbl > tbody > tr:nth-child(" + checkBoxPosition + ") > td > div > input")).click();

Full method:

public void selectOption(int positionOfCheckBox){
        By locator = By.cssSelector("table.a-cptp-tbl > tbody > tr:nth-child(" + positionOfCheckBox + ") > td > div > input");
        //wait for your element to be visible
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
        //click element after it is visible/clickable
        driver.findElement(locator).click();
    }
optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
-2

Just add Thread.sleep(5000); above your script for radio button. For example like this

     Thread.sleep(5000);
     driver.findElement(By.id("uniform-id_gender2")).click();

It works for me. :)

Alexander
  • 4,420
  • 7
  • 27
  • 42