Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择Selenium Webdriver(Java)上的单选按钮_Java_Selenium_Selenium Webdriver - Fatal编程技术网

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

选择Selenium Webdriver(Java)上的单选按钮,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我想在这样一个测试web应用程序上选择一个单选按钮。经过反复尝试,我找不出怎么做 以下是web应用程序的HTML <ol class="plain block-listing solid choice-area"> <li class="qti-choice qti-simpleChoice"data-serial="choice_simplechoice_56c2a110874b8101352859" data-identifier="choice_1">

我想在这样一个测试web应用程序上选择一个单选按钮。经过反复尝试,我找不出怎么做

以下是web应用程序的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>
有人能帮忙吗?
提前谢谢。

您可以使用单选按钮的输入标签进行选择 试试这个: WebElement choiceOption=driver.findElement(按.tagName(“输入”));
选择选项。单击()

您可以使用相对路径,而不是使用绝对路径,并且有多种方法来定位此webelement。 最简单的xpath之一是: findElement(By.xpath(“//input[@value='choice_1']))。单击()

是相同的答案

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

你可以用多种方法来做

  • 获取所有选项元素,循环并单击具有所需值的元素

    列表选项=driver.findElements(By.css(“ol.plain.block-listing.solid.choice-area输入”) 用于(选项中的选项){ 如果(选项值==“选项2”){ 如果(!option.isChecked())选项。单击(); 打破 } }

    }

  • 一个简单的方法是,你可以用你想要点击的值构建一个动态的x路径或css。例如,如果选择2是我们想要点击的

    WebElement option=driver.findElement(By.css(输入[value='choice_2']); 如果(!option.isChecked())选项。单击()


  • 希望有帮助!

    首先在方法中,找到ol标记的Webelement,在ol find List of input tag和apply for loop for input tag List中,并与要单击的input tag的值匹配

    请尝试以下代码:

    `

    myMethod(字符串输入值){
    WebElement olTag=driver.findElement(按.tagName(“ol”));
    List inputTagList=olTag.findElements(按.tagName(“输入”));
    用于(WebElement项目:inputTagList){
    如果(item.getAttribute(“value”).equals(InputAgValue)){
    项。单击();
    打破
    }
    }
    }
    

    `

    “//html/body/div[1]/div[1]/div/div/div/ol/li[1]/div/label/span”
    不易维护。几乎任何结构更改都会破坏您的测试。请注意,谢谢您的建议。Hi Deo,我尝试了您的建议,但仍然返回了NoTouchElement例外。谢谢您的帮助。Hi Prakash,我会尝试这个。谢谢您的帮助Hi Randhika,我会尝试这个。谢谢您的帮助。Hi Sandeep,我会谢谢你的帮助。嗨,普密斯,我试试这个。谢谢你的帮助。
    driver.findElement(By.xpath("//label[@class='real-label']//*[@value='choice_2']")).click();
    //The above code selects the option "Pelabuhan"
    
    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;
                    }
        }
    }