简体   繁体   English

使用Selenium Webdriver选择PrimeFaces单选按钮

[英]Select a PrimeFaces radio button with Selenium Webdriver

I am trying to write Selenium tests to select a radio button. 我正在尝试编写Selenium测试来选择一个单选按钮。 Below is the html from 'view Source'. 以下是'view Source'中的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. 我想选择“上传调查网址”单选按钮。

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). 错误元素在点(809,367)处不可点击。 Other element would receive the click: ... 其他元素会收到点击:...

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? 我错过了什么?

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

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

Try 尝试

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

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

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

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