XPath / CSS / Locator使用WebDriver选择标题单选按钮选项

时间:2018-11-21 10:49:02

标签: java selenium selenium-webdriver xpath webdriverwait

是否存在以下问题的代码?我无法编写正确的XPath或CSS为下面的代码选择标题“ MRS”。我正在使用以下代码,但无法执行。

driver.findElement(By.xpath(“ // div [@ value ='MRS']”)))。click();

<div class="question-controls clearfix">
   <div class="question-group ">
        <label class="off">Mr</label>
        <input type="radio" name="title" value="MR" class="ui-helper-hidden-accessible">

        <label class="off">Mrs</label>
        <input type="radio" name="title" value="MRS" class="ui-helper-hidden-accessible">

        <label class="on">Miss</label>
        <input type="radio" name="title" value="MISS" class="ui-helper-hidden-accessible">

        <label class="off">Ms</label>
        <input type="radio" name="title" value="MS" class="ui-helper-hidden-accessible">

        <label class="off">Dr</label>
        <input type="radio" name="title" value="DR" class="ui-helper-hidden-accessible">    
        <label class="off">Rev</label>
        <input type="radio" name="title" value="REV" class="ui-helper-hidden-accessible">    
        <input type="text" maxlength="20" style="display:none" name="otherTitle" value="" placeholder="Other title:" id="otherTitle">
    </div>          
</div>

参考链接:

https://sqa.stackexchange.com/q/36421/35535

2 个答案:

答案 0 :(得分:0)

//div[@class='question-group']/input[@type='radio'][@value='MRS']   

如果上述xpath不起作用,请查看您要查找的元素是否在任何iframe中,在这种情况下,请先切换到该框架,然后尝试选择此单选按钮。

driver.switchTo().frame(driver.findElement(By.xpath("iframexpath here")));

答案 1 :(得分:0)

根据您提供的 HTML ,您可以在与标题 MRS 相关的元素上调用click(),因为该元素具有clearfix类,您需要诱使 WebDriverWait 使所需的元素可点击,并且可以使用以下解决方案:

  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='question-controls clearfix']/div[@class='question-group']//label[contains(.,'Mrs')]//following::input[1]"))).click();