【问题标题】:Selecting Radio Buttons with XPath in C#在 C# 中使用 XPath 选择单选按钮
【发布时间】:2015-05-27 00:02:48
【问题描述】:

我正在尝试使用 XPath 命令选择一个特定的单选按钮:

driver.FindElement(By.XPath("//td[contains(@input id,   'SearchTypePatientNameDob')]")).Click();

包含我想要的按钮的列表如下:

<tr>
    <td>
    <input id="RadioButtonSearchTypePatientNameDob" type="radio"      value="SearchTypePatientNameDob" name="SearchType">
    <span class="Instructions">Patient Name / Patient Date of Birth</span>
    </td>
</tr>

我的命令找不到选择/单击它的按钮。任何建议都会大有裨益。

【问题讨论】:

    标签: c# selenium xpath selenium-webdriver radio-button


    【解决方案1】:

    您需要找到input 元素,而不是td 元素:

    //input[contains(@id, 'SearchTypePatientNameDob')]
    

    请注意,我看不出你为什么不使用简单的By.Id 定位器的任何正当理由:

    driver.FindElement(By.Id("RadioButtonSearchTypePatientNameDob")).Click();
    

    您可能还需要明确wait for the element to be present

    【讨论】:

    • @Rinktacular 好吧,可能有多种原因。首先,您在页面上看到任何 iframe/frame 元素吗?谢谢。
    • @Rinktacular 我还添加了一个指向答案的链接,提供了等待元素出现的方法。
    • @Rinktacular 是的,如果元素在 iframe 内,为了定位元素,您需要使用 driver.SwitchTo().Frame(&lt;frame name or id or index or an element referring to the frame&gt;) 切换到 iframe
    • @Rinktacular nope,driver.SwitchTo().Frame("childFrame");
    • @Rinktacular 是页面上唯一的 iframe 吗?如果是,您可能需要等待此 iframe 元素出现在 DOM 中(按照答案中的链接)。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多