java - 使用 Selenium Webdriver 选择 PrimeFaces 单选按钮

标签 java selenium selenium-webdriver

我正在尝试编写 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()

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

最佳答案

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

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

关于java - 使用 Selenium Webdriver 选择 PrimeFaces 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20645800/

相关文章:

python - 网站检测到 selenium webdriver

c# - 通过 Selenium WebDriver C# 获取 HTTP 状态码

python - Selenium:driver.get_cookies() 返回不完整的 cookie 列表

java - 使用@JoinTable映射时如何指定FK名称?

java - @Test方法不等待selenium代码完成其执行

java - 如何使用 Selenium 将值输入到表格中的一组文本字段

selenium - 在 selenium webdriver 中的 "ul"html 代码的 xpath 中出现问题

Java,OpenCV 3.0 中的 "Highgui.CV_CAP_PROP_FRAME_HEIGHT"

java - 当整行为空时如何不将数据放入 map 中

java - 违反外键约束 JPA