繁体   English   中英

Selenium Webdriver:单击单选按钮不起作用

[英]Selenium Webdriver: Click on radio button not working

我有一个点击单选按钮的代码,起初我使用的是Chrome。 使用以下代码:

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

我收到了错误:

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

做研究,我把代码更改为:

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

现在,我正在尝试使用Internet Explorer驱动程序。 但它不会执行点击。

我尝试了以下方法:

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

但都没有效果。 第一个只关注按钮,所以我添加了另一个sendKeys,但它不起作用。 第二和第三,没有任何反应。

编辑:

添加HTML代码段。

<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>

当我点击单选按钮时,标签会在点击后获得额外的属性。

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

附加编辑:

这组按钮看起来像这样:

在此输入图像描述

如前所述,一个按钮+标签块具有以下HTML结构:

<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>

单击按钮后,相应的标签将获得其他属性:

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

似乎AWMouseDown似乎是“正式”点击按钮的触发器。

编辑:

表格的完整HTML代码段。 (请注意,如果我提交了一个表,那么该表已经被清理了,所以对于某些错误道歉。)

<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>

尝试使用如下的JavaScript:

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

如果您使用的是QMetry Automation Framework,则应创建自定义单选按钮组件,例如您可以使用此类自定义实现覆盖click方法。

使用ExplicitWait等待元素直到可单击,然后必须单击该元素

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

     element.click();

EDITED

如果它在IE浏览器中引起问题。 阻止在IE浏览器中找到元素的原因是ActiveX Controls

所以只需要按照以下步骤操作 -

  1. 转到Internet选项>高级>安全性并检查下面提到的检查 - 在此输入图像描述

  2. 检查后>申请,然后不要忘记重新启动您的电脑

现在只需运行您的脚本并尝试使用id单击该元素

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

希望这会奏效。 如果仍然面临同样的问题,请告诉我们。

您可以尝试使用列表识别单选按钮,然后使用带有get()的索引单击列表中的元素吗?

List<WebElement> radioGrp = driver.findElements(By.name("xxxxxxxx"));
radioGrp.get(0).click();

不确定是什么导致了这个问题。它对我有用:

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

似乎单选按钮是<input><label>标签的组合,即<div> with class="w-rdo-container"或其<td> parent。 我是这么认为的,因为说唱歌手<td>和标签Option 2所在的<td>是兄弟姐妹。

class="w-rdo-container"似乎不是唯一的,所以你可以使用xpathid="button1"上传html树

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

编写一个接受单选按钮位置的方法,然后使用cssSelector单击按钮,如下所示:

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

完整方法:

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

只需添加Thread.sleep(5000); 在单选按钮的脚本上方。 比如这样

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

这个对我有用。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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