【问题标题】:Select a PrimeFaces radio button with Selenium Webdriver使用 Selenium Webdriver 选择 PrimeFaces 单选按钮
【发布时间】:2014-09-23 23:45:38
【问题描述】:

我正在尝试编写 Selenium 测试来选择单选按钮。以下是来自“查看源代码”的 html。

<table id="surveyForm:surveyUrlType" class="ui-selectoneradio ui-widget" style="width:100%;margin-top:10px;margin-bottom: 10px;">
    <tbody>
    <tr>
        <td>
            <div class="ui-radiobutton ui-widget">
                <div class="ui-helper-hidden-accessible">
                    <input id="surveyForm:surveyUrlType:0" name="surveyForm:surveyUrlType" type="radio" value="TYPED" checked="checked" onchange="com.ssi.feasibility.surveyView.showSurveyType(this);">
                </div>
                <div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default ui-state-active">
                    <span class="ui-radiobutton-icon ui-icon ui-icon-bullet"></span>
                </div>
            </div>
        </td>
        <td><label for="surveyForm:surveyUrlType:0">Enter Survey URL</label></td>
        <td>
            <div class="ui-radiobutton ui-widget">
                <div class="ui-helper-hidden-accessible">
                    <input id="surveyForm:surveyUrlType:1" name="surveyForm:surveyUrlType" type="radio" value="FILE" onchange="com.ssi.feasibility.surveyView.showSurveyType(this);">
                </div>
                <div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default">
                    <span class="ui-radiobutton-icon"></span>
                </div>
            </div>
        </td>
        <td><label for="surveyForm:surveyUrlType:1">Upload Survey URLs</label></td>
    </tr>
    </tbody>
</table>

我想选择“上传调查 URL”单选按钮。

我尝试了几种不同的方法来选择单选按钮。以下是一些:

        $("#surveyForm\\surveyUrlType").click();

        This gives me the error : 
        $("#surveyForm\\:surveyUrlType\\:1").first().click()

错误 - 元素在点 (809, 367) 处不可点击。其他元素会收到点击:...

下面给我NoSuchElementFound:

        driver.findElement(By.id("surveyForm:surveyUrlType:1")).click()
        driver.findElement(By.xpath("//input[@type='radio' and @id='surveyForm\\:surveyUrlType\\:1']")).click()
        driver.findElement(By.xpath("//input[@value='FILE']")).click()
        driver.findElement(By.cssSelector("#surveyForm\\:surveyUrlType\\:1")).click()

下面不要给我任何错误,但他们也没有选择单选按钮。

        $(".ui-radiobutton-box ui-widget ui-corner-all ui-state-default")[1].click()
        $(".ui-radiobutton-box ui-widget ui-corner-all ui-state-default").click()
        $("#surveyForm\\:surveyUrlType").find(".ui-radiobutton-box ui-widget ui-corner-all ui-state-default").find("span").click()

但这些都不起作用。我错过了什么?

【问题讨论】:

  • 它在 Primefaces 对话框中。我可以访问页面上的其他输入框并操作它们的值。但只是这个单选按钮。第一个是预选的。我想选择第二个。不,我没有尝试过 WebDriverWait。

标签: java selenium selenium-webdriver


【解决方案1】:

这就是我终于让它工作的方式!

 driver.findElement(By.cssSelector("label[for='surveyForm\\:surveyUrlType\\:1']")).click()

【讨论】:

  • 该代码看起来像您单击的是 label 而不是 input。真的有用吗?
  • @vincebowden 是的,label 有一个 onclick 监听器。我不确定为什么其他选项会失败,但是单击标签对我来说适用于 Selenium 2.47.1 和 Prime 5.2
【解决方案2】:

试试

WebElement element = new WebDriverWait(Driver,30).until(ExpectedConditions.elementToBeClickable(By.id("surveyForm:surveyUrlType:1")));
element.click();

【讨论】:

  • 感谢您的回复。我试过了,我得到了这个错误:org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (454, 255)。其他元素会收到点击:
    ...
    (会话信息:chrome=31.0.1650.63)(驱动程序信息:chromedriver=2.1,platform=Mac OS X 10.8.5 x86_64)(警告:服务器未提供任何堆栈跟踪信息)
【解决方案3】:

我在 Java 中有以下代码

lst=driver.findElements(by_name("surveyForm:surveyUrlType"))
   for(condition)
      option.click()

在python中我通过这种方式解决了上面的问题:

lst=driver.find_elements_by_name("surveyForm:surveyUrlType")
for i in lst:
   if(i.text=="Enter Survey")
       i.click()
   elif(i.text=="Upload Survey URLs")
       i.click()

如果上面的没有帮助尝试这个

driver.findElement(by_class_name("ui-radiobutton-icon ui-icon ui-icon-bullet")).click()
driver.findElement(by_class_name("ui-radiobutton-icon")).click()

【讨论】:

  • 感谢您的回复。我尝试了所有建议,但没有奏效。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多