Selenium、XPATH 在表中搜索元素并单击单选按钮 [英] Selenium, XPATH to search for an Element in a table and Click on Radio button

查看:37
本文介绍了Selenium、XPATH 在表中搜索元素并单击单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从表条目列表中选择一个项目,然后单击单选按钮.单选按钮在左侧.在下面的 HTML 代码中唯一用于查找选择的唯一 ID 是TestSucscription"我需要在表格中搜索此值(它可以有多个行,并且可以在任何位置),然后单击其左侧的单选按钮.

<td><input class="" type="radio" value="2ba068ff-b797-444e-b56b-a0ae0ae06f06" name="subscription[id]"><div class="隐藏"></div></td><td>测试订阅<td></td><td>测试</td></tr><tr><td><input class="" type="radio" value="36a87684-9a65-4455-8605-dc5b5368fc23" name="subscription[id]"><div class="隐藏"><span class="error error-msg">不能为空.</span>

</td><td>安全保护</td><td></td><td>CPN ChildProtection CPN ChildProtection</td></tr>

请帮我解决这个问题.我在 Webdriver(Java) 中尝试了以下选项

driver.findElement(By.xpath("//td[contains(text(), 'TestSubscription')]]/td/input[@type='radio']")).click();

我想我会从树上下来然后回来点击单选按钮,这就是它失败的原因.

解决方案

您可以通过以下 xpath 找到单选按钮:

//td[contains(text(), 'TestSubscription')]/../td/input

这将 td 与文本匹配,上升到父级,然后遍历您要单击的 input 控件.

I need to select an Item from the list of Table entries and click on Radio button. Radio button is on the left. In the HTML Code below Only Unique ID to look for a selection is the "TestSucscription" I need to grep for this Value in the Table(It can have multiple Rows and It can be in any position) and then click on the Radio button on to its left.

<tr>
    <td>
        <input class="" type="radio" value="2ba068ff-b797-444e-b56b-a0ae0ae06f06" name="subscription[id]">
        <div class="hide"></div>
    </td>
    <td> TestSubscription</td>
    <td></td>
    <td>Testing</td>
</tr>
<tr>
    <td>
        <input class="" type="radio" value="36a87684-9a65-4455-8605-dc5b5368fc23" name="subscription[id]">
        <div class="hide">
            <span class="error error-msg">This cannot be left blank.</span>
        </div>
    </td>
    <td>SecurityProtection</td>
    <td></td>
    <td>CPN ChildProtection CPN ChildProtection</td>
</tr>

Please help me with this. I have tried below options in Webdriver(Java)

driver.findElement(By.xpath("//td[contains(text(), 'TestSubscription')]]/td/input[@type='radio']")).click();

I think Im going down the tree and coming back to click on Radio button which is why Its failing.

解决方案

You can find the radio button with the following xpath:

//td[contains(text(), 'TestSubscription')]/../td/input

This matches the td with the text, goes up to the parent, then traverses to the input control you want to click.

这篇关于Selenium、XPATH 在表中搜索元素并单击单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆