Java 使用 Selenium Webdriver 选择 PrimeFaces 单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20645800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:19:38  来源:igfitidea点击:

Select a PrimeFaces radio button with Selenium Webdriver

javaseleniumselenium-webdriver

提问by user1860447

I am trying to write Selenium tests to select a radio button. Below is the html from 'view Source'.

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

I want to select 'Upload Survey URLs' radio button.

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

I have tried several different approaches to select the radio button. Here are a few:

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

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

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

Error- Element is not clickable at point (809, 367). Other element would receive the click: ...

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

Below give me NoSuchElementFound:

下面给我 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()

Below don't give me any error but they also do not select the radio button.

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

        $(".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()

But none of these work. What am I missing?

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

采纳答案by user1860447

This is how I got it to work finally !!

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

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

回答by Yuvaraj HK

Try

尝试

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

回答by Mahesh Reddy Atla

I have the following code in Java

我在 Java 中有以下代码

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

In python I solved the problem above this way:

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

If the above one haven't helped try this one

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

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