如何从选定的单选按钮读取文本以及如何选择另一个单选按钮?

时间:2018-07-03 10:05:54

标签: selenium selenium-webdriver

我想读取所选的单选按钮文本,并且想要选择另一个单选框,因此我正在读取所有3个单选按钮文本并传递给字符串,并尝试选择未选中的单选按钮,但如果条件不满足则使用能够做到这一点,请帮助我选中该复选框。

这是HTML代码

<ul class="custom-ul">
    <li class="custom-li custom-radio m-subscriptions-padding receive-VIP1">
        <input name="email-preferences" id="receive-VIP" type="radio" value="receive-VIP1">
        <input name="_D:email-preferences" type="hidden" value=" ">                       <label for="receive-VIP" class="custom-radio-label">
             <span class="label-alt-text">Receive special offers via email</span>
        </label>
    </li>
    <li class="custom-li custom-radio m-subscriptions-padding receive-fewer1">
        <input name="email-preferences" checked="checked" id="receive-fewer" type="radio" value="receive-fewer1" class="radio-checked">
        <input name="_D:email-preferences" type="hidden" value=" ">
        <label for="receive-fewer" class="custom-radio-label">
             <span class="label-alt-text">Receive fewer emails. We won't email you more than one time per week.</span>
        </label>
    </li>
    <li class="custom-li custom-radio m-subscriptions-padding receive-no-emails">
        <input name="email-preferences" id="receive-no-emails" type="radio" value="receive-no-emails">
        <input name="_D:email-preferences" type="hidden" value=" ">
        <label for="receive-no-emails" class="custom-radio-label">
             <span class="label-alt-text"> Unsubscribe: we will remove you from all testdevv promotional emails.</span>
        </label>
        </li>
</ul>

3 个答案:

答案 0 :(得分:0)

尝试-

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class A {

    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("url here");
        List<WebElement> radioButtons = driver.findElements(By.name("email-preferences"));
        for (int i = 0; i < radioButtons.size(); i++) {
            if (driver.findElement(By.name("email-preferences")).isSelected()) {
                System.out.println(radioButtons.get(i).getText());
                break;
            }
        }
    }

}

答案 1 :(得分:0)

您可以找到选定的选项文本以及所有可用的选项列表,如下所示。

请在您的代码中使用以下逻辑

代码:

    //If none of the option is selected, then exception will be thrown and to avoid this we can use findElements method
    List<WebElement> radioButtonSelectedList=driver.findElements(By.xpath("//*[@class='radio-checked']//parent::*//span"));
    String selectedValue="";
    ArrayList<String> optionsList = new ArrayList<String>();

    if(radioButtonSelectedList.size()!=0){
        //If the button is selected, then we can get the selected option text
        selectedValue=radioButtonSelectedList.get(0).getText();

        //To get all the available Options
        List<WebElement> optionsElement=driver.findElements(By.xpath("//ul[@class='custom-ul']//span"));

        for(WebElement element: optionsElement){
            optionsElement.add(element);
        }

    }
    else{
        System.out.println("None of the option is selected");
    }

    System.out.println("Available Options :"+optionsList);
    System.out.println("Selected Option :"+selectedValue);

答案 2 :(得分:0)

您可以尝试下一个解决方案:

    //this is row with radiobutton and text
    private By customRadioElement = By.xpath("//li[contains(@class, 'custom-radio')]");
    //this is all radiobuttons
    private By radiobutton = By.xpath("./input[@name='email-preferences']");
    //this is all labels for radiobuttons
    private By radiobuttonLabel = By.xpath("./label/span");

    public void selectRadiobutton(String labelText) {
        List<WebElement> tempList = driver.findElements(customRadioElement);
        for (WebElement element : tempList) {
            //webElement can search for elements, and if xpath locator starts with "." element will search for element from his children
            if (labelText.equals(element.findElement(radiobuttonLabel).getText())) {
                element.findElement(radiobutton).click();
            }
        }
    }

    public String getSelectedRadiobuttonLabelText() {
        List<WebElement> tempList = driver.findElements(customRadioElement);
        for (WebElement element : tempList) {
            //webElement can search for elements, and if xpath locator starts with "." element will search for element from his children
            if (element.findElement(radiobutton).isSelected()) {
                return element.findElement(radiobuttonLabel).getText();
            }
        }
        return "";
    }