bugfix> selenium > 投稿

私は選択したラジオボタンのテキストを読み、別のラジオボックスを選択したいので、3つのラジオボタンのテキストをすべて読み、文字列に渡して、選択されていないラジオボタンを選択しようとしていますが、if条件は使用できませんそれ、チェックボックスを選択するのを手伝ってください。

これは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 件
  • 試してください-

    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;
                }
            }
        }
    }
    
    

  • 以下のように、選択したオプションテキストと使用可能なすべてのオプションリストを見つけることができます。

    コードで以下のロジックを使用してください

    コード:

       //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);
    
    

  • 次の解決策を試すことができます:

       //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 "";
        }
    
    

あなたの答え