简体   繁体   中英

Selenium Webdriver: Click on radio button not working

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:

在此输入图像描述

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>

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.

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 - 在此输入图像描述

  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.

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

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

It seems that the radio button is combination of the <input> and <label> tags, ie 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

点击Option 2单选按钮,尝试以下操作:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM