【问题标题】:Selenium Webdriver: Click on radio button not workingSelenium Webdriver:单击单选按钮不起作用
【发布时间】:2017-05-23 07:44:42
【问题描述】:

我有一个点击单选按钮的代码,起初我使用的是 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,但它不起作用。第 2 次和第 3 次,什么都没有发生。

编辑:

添加 HTML sn-p。

<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 sn-p。 (请注意,这张桌子已经被清理过了,如果我犯了一些错误,我们深表歉意。)

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

【问题讨论】:

  • 你能加点htmlsn-p吗?
  • 嗨@NarendraRajput,我添加了HTML sn-p。
  • 你试过ExplicitWait吗?
  • 你试过其他浏览器吗?如果可行,请尝试在 IE 浏览器中将缩放级别设置为 100%。
  • 是的,它可以在 Google Chrome 中使用,如上所述。我已经将 IE 设置为 100% 缩放级别。

标签: java selenium


【解决方案1】:

尝试使用 JavaScript,如下所示:

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

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

【讨论】:

  • arguments[0].click() 不起作用,但 arguments[0].checked = true 成功了!谢谢!
  • arguments[0].click() 对我有用,谢谢您的回答
【解决方案2】:

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

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

     element.click();

已编辑

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

所以你只需要遵循这些步骤 -

  1. 转到 Internet 选项 > 高级 > 安全并检查以下提到的检查 -

  2. 检查后>申请,然后别忘了重启你的电脑

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

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

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

【讨论】:

【解决方案3】:

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

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

【讨论】:

  • 嗨,我试过了,radioGrp 为空。我很确定我输入了正确的名称。
  • 嗨,我再次尝试等待页面加载并成功获取单选按钮,但点击不起作用。
【解决方案4】:

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

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

【讨论】:

  • 那是 PhantomJS,但是 OP 说他使用的是 IE。
【解决方案5】:

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

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

【讨论】:

  • 嗨,谢谢。我都试过了,但似乎没有用。我不确定 xpath 是否适用于 IEDriver?
  • @KawamotoTakeshi 你有什么错误吗?还是什么都没发生?
  • @KawamotoTakeshi 您也可以尝试以其他方式定位这些元素,我只能根据您提供的 html sn-ps 来回答。
  • 嗨,谢谢。我尝试在 Chrome 中再次查看它的反应,奇怪的是,你可能走在了正确的轨道上。点击相应的 TD 仍然会触发“点击”。我会尝试看看如何在没有 id 的情况下获得 TD。
  • 您好,我已将其更改为 CSS 选择器,但没有任何反应。代码像单击 td/div 一样继续进行,但没有任何反应。 Eclipse 中没有错误/堆栈跟踪。
【解决方案6】:

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

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

【讨论】:

    【解决方案7】:

    编写一个接受单选按钮位置的方法,并使用 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();
        }
    

    【讨论】:

      【解决方案8】:

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

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

      它对我有用。 :)

      【讨论】:

      • 硬睡眠不是一个好主意,应该改用 WebDriverWait。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多