简体   繁体   English

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

[英]XPath/CSS/Locators to select title radio button option using WebDriver

Is there any code for below issues? 是否存在以下问题的代码? I am unable to write the correct XPath or CSS to select the title "MRS" for below code. 我无法编写正确的XPath或CSS为下面的代码选择标题“ MRS”。 I am using below code, but unable to execute it. 我正在使用下面的代码,但无法执行它。

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

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

link for reference: 参考链接:

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

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

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

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

As per the HTML you have provided to invoke click() on the element related to the title MRS as the element is having the clearfix class, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution: 按照您提供的HTML要求 ,当元素具有clearfix类时,在与标题MRS相关的元素上调用click() ,您需要诱导WebDriverWait使所需的元素可单击,并且可以使用以下解决方案:

  • xpath : 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(); 

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

相关问题 Webdriver CSS定位器突然停止在IE11上运行,而xpath定位器可以正常工作 - Webdriver CSS locators suddenly stopped working on IE11, while xpath locators do work 为什么我没有使用selenium webdriver选择单选按钮? - Why am failing to select radio button using selenium webdriver? 如何通过不使用x路径来选择Selenium Webdriver中的单选按钮 - how to select radio button in selenium webdriver by not using x path 使用带有Java的Selenium WebDriver在组中选择单选按钮 - Select Radio Button in a group using Selenium WebDriver with Java 如何使用 selenium webdriver 在 JQGrid 中选择单选按钮 - How to select Radio button in JQGrid using selenium webdriver 如何使用带有Java的Selenium WebDriver选择单选按钮? - How to select radio button using Selenium WebDriver with Java? 想用JSP核心标识元素 <c:if> 使用Xpath或其他任何定位器使用Selenium Webdriver标记 - want to identify element with JSP core<c:if> tag using selenium webdriver using xpath or any other locators 如何在Selenium Webdriver中选择单选按钮? 动态选择单选按钮 - How to select radio button in selenium webdriver ? dynamic select of radio button 使用Selenium Webdriver选择PrimeFaces单选按钮 - Select a PrimeFaces radio button with Selenium Webdriver 无法在网页中找到定位器(Xpath,Css) - Unable to find locators(Xpath,Css) ,in the webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM