【问题标题】:XPath/CSS/Locators to select title radio button option using WebDriverXPath/CSS/Locators 使用 WebDriver 选择标题单选按钮选项
【发布时间】:2018-11-21 10:49:01
【问题描述】:

以下问题是否有任何代码?我无法编写正确的 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

【问题讨论】:

  • 我疯了还是您的“参考链接”是您问题的链接?我们需要一个指向该页面的链接。
  • - 我看到一个没有收音机的登录页面。
  • 需要登录才能看到该页面,我不能提供登录详细信息
  • 那你可能得不到好的答案,抱歉。

标签: java selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

根据您提供的 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();
    

【讨论】:

  • 您能否使用准确的错误跟踪日志更新主要问题以进行进一步分析?
【解决方案2】:
//div[@class='question-group']/input[@type='radio'][@value='MRS']   

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

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

【讨论】:

  • 尝试了您的代码,但仍然收到错误消息。请注意该元素不在任何 iframe 中。 driver.findElement(By.xpath("//div[@class='question-group']/input[@type='radio'][@value='MRS']")).click();跨度>
  • List radioList =driver.findElements(By.xpath("//div[@class='question-group']/input[@type='radio']")); for(int i=0;i
  • 我使用了上面的代码,但由于 if 循环中的 radioList,我无法执行代码。消息是 radioList 无法解析或不在字段中。下面使用代码 ListradioList =driver.findElements(By.xpath("//div[@class='question-group']/input[@type='radio']")); for(int i=0;i
  • 抱歉有更正,ListradioList =driver.findElements(By.xpath("//div[@class='question-group']/input[@type='radio ']")); for(int i=0;i
  • 由于 if 循环中的 getElement 无法执行。当我点击它时的消息:方法 getElement(int) 未定义类型 List
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 2018-12-20
  • 1970-01-01
相关资源
最近更新 更多