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>
7
  • can you add some html snippet ? Commented Jan 9, 2017 at 5:26
  • Hi @NarendraRajput, I added the HTML snippet. Commented Jan 9, 2017 at 5:33
  • Have you tried ExplicitWait ? Commented Jan 9, 2017 at 5:58
  • Have you tried in other browsers? if that works try by setting zoom level to 100% in IE browser. Commented Jan 11, 2017 at 5:24
  • Yes, it works in Google Chrome, as stated above. I already have IE set to 100% zoom level. Commented Jan 11, 2017 at 5:25

8 Answers 8

10
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

arguments[0].click() doesn't work but arguments[0].checked = true did the trick! Thanks!
arguments[0].click() works for me,Thank you for your answer
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.

2 Comments

Hi, thanks. It doesn't work. The explicit wait always timed out.
Might be there are many cause as you mentioned error Element is not clickable . follow this post stackoverflow.com/questions/11908249/…
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();

2 Comments

Hi, I tried this and radioGrp is null. I'm pretty sure I put the correct name.
Hi, I tried it again waiting for the page to load and it successfully gets the radio buttons but the click is not working.
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();

1 Comment

That's PhantomJS, but the OP stated that he is using IE.
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

8 Comments

Hi, thanks. I tried both and it didn't seem to work. I am not sure if xpath is applicable for IEDriver?
@KawamotoTakeshi Did you get any errors? or nothing happened?
@KawamotoTakeshi You could also try locating those elements some other way, I can base my answer only on the html snippets you provided.
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.
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.
|
0

Try following for clicking on Option 2 radio button:

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

Comments

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

Comments

-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. :)

1 Comment

Never a good idea to put hard sleeps, one should use WebDriverWait instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.