简体   繁体   English

选择Selenium Webdriver(Java)上的单选按钮

[英]Selecting Radio button on Selenium Webdriver (Java)

I want to selecting one of radio button on such a test web application. 我想在这样的测试Web应用程序上选择一个单选按钮。 after try and error i couldn't find out how to do it. 尝试和错误后,我找不到如何做到这一点。

Here is the HTML of the web apps. 这是网络应用程序的HTML。

    <ol class="plain block-listing solid choice-area">
    <li class="qti-choice qti-simpleChoice"data-serial="choice_simplechoice_56c2a110874b8101352859" data-identifier="choice_1">
    <div class="pseudo-label-box">
    <label class="real-label">
    <input type="radio" value="choice_1" name="response-interaction_choiceinteraction_56c2a1108692f930922645">
    <span class="icon-radio"></span>
    </label>
    <div class="label-box">
    <div class="label-content clear" contenteditable="false">
    <div class="qti-block">a. Terminal</div>
    </div>
    </div>
    </div>
    </li>
    <li class="qti-choice qti-simpleChoice" data-serial="choice_simplechoice_56c2a110878f8127430456" data-identifier="choice_2">
    <div class="pseudo-label-box">
    <label class="real-label">
    <input type="radio" value="choice_2" name="response-interaction_choiceinteraction_56c2a1108692f930922645">
    <span class="icon-radio"></span>
    </label>
    <div class="label-box">
    <div class="label-content clear" contenteditable="false">
    <div class="qti-block">b. Pelabuhan</div>
    </div>
    </div>
    </div>
    </li>

On my selenium Webdriver, here is my code : 在我的Selenium Webdriver上,这是我的代码:

    WebElement choiceOption = driver.findElement(By.xpath("//html/body/div[1]/div[1]/div/div/div/ol/li[1]/div/label/span"));
    choiceOption.click();

Anyone can help? 有人可以帮忙吗? Thank you in advance. 先感谢您。

You can use input tag of radio button to select it Try this : WebElement choiceOption = driver.findElement(By.tagName("input")); 您可以使用单选按钮的输入标签来选择它。尝试以下操作:WebElement choiceOption = driver.findElement(By.tagName(“ input”)); choiceOption.click(); choiceOption.click();

Instead of using an absolute path, you can go with relative path and there are multiple ways to locate this webelement. 除了使用绝对路径,您还可以使用相对路径,并且有多种方法可以定位此网络元素。 One of the simplest xpath would be : driver.findElement(By.xpath("//input[@value='choice_1']")).click(); 最简单的xpath之一是:driver.findElement(By.xpath(“ // input [@ value ='choice_1']”))。click();

Yes same answer 是的,同样的答案

driver.findElement(By.xpath("//label[@class='real-label']//*[@value='choice_2']")).click();
//The above code selects the option "Pelabuhan"

You can do it multiple ways. 您可以通过多种方式进行操作。

  1. Get all option elements, loop through and click the one with the value you need. 获取所有选项元素,遍历并单击具有所需值的元素。

    List options = driver.findElements(By.css("ol.plain.block-listing.solid.choice-area input") for(option in options){ if(option.value=="choice_2"){ if(!option.isChecked()) option.click(); break; } } 列表选项= driver.findElements(By.css(“ ol.plain.block-listing.solid.choice-area输入”)for(选项中的选项){if(option.value ==“ choice_2”){if(! option.isChecked())option.click(); break;}}

    } }

  2. A simple way is, you can build a dynamic x-path or css with the value you wanted to click.. For example if choice_2 is what we wanted to click 一种简单的方法是,您可以使用要单击的值构建动态的x路径或CSS。例如,如果choice_2是我们要单击的内容

    WebElement option = driver.findElement(By.css(input[value='choice_2'])); WebElement选项= driver.findElement(By.css(input [value ='choice_2']))); if(!option.isChecked()) option.click(); if(!option.isChecked())option.click();

Hope that helps! 希望有帮助!

In a method first, find Webelement of ol tag, in ol find List of input tag and apply for loop for input tag list and match with value of the input tag to click 首先在方法中,找到ol标签的Webelement,在ol中找到输入标签的列表,并为输入标签列表申请循环并与输入标签的值匹配以单击

Try below code: 试试下面的代码:

` `

myMethod(String inputTagValue){
WebElement olTag = driver.findElement(By.tagName("ol"));
List<WebElement> inputTagList = olTag.findElements(By.tagName("input"));
for (WebElement item: inputTagList ) {
                if(item.getAttribute("value").equals(inputTagValue)){
                    item.click();
                    break;
                }
    }
}

` `

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

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