简体   繁体   English

如何在Selenium Webdriver中选择单选按钮? 动态选择单选按钮

[英]How to select radio button in selenium webdriver ? dynamic select of radio button

<tbody>
<tr class="odd">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4493B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4493B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:dece:cs</td>
</tr>
<tr class="even">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4494B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4494B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:coord:cs</td>
</tr>
<tr class="odd">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4495B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4495B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:300</td>
</tr>
<tr class="even">
<td>
<input id="nodeAccountOid" type="radio" onclick="setNodeAccountIdToCredentialCheck('E9E2930C4495B569E040A8C0158E4ABD');" style="width:100%;border:0px">
</td>
<td>E9E2930C4495B569E040A8C0158E4ABD</td>
<td>monacho1</td>
<td>urn:dece:org:org:dece:10</td>
</tr>
</tbody>
</table>

i want to select the radio button corresponding to urn:dece:org:org:dece:10 which is fourth row in the html provided. 我想选择对应于urn:dece:org:org:dece:10的单选按钮,这是所提供的html中的第四行。 the row may change sometimes based on some inputs in AUT. 根据AUT中的某些输入,有时该行可能会更改。 please provide me the way to select it. 请提供给我选择的方式。

thanks in advance 提前致谢

Since the text never changes, you can use that as a starting point within the DOM, and use XPath to navigate through to the input you need: 由于文本永远不变,因此您可以将其用作DOM中的起点,并使用XPath导航至所需的input

//td[.='urn:dece:org:org:dece:10']/parent::tr/descendant::input[@id='nodeAccountOid' and @type='radio']
  1. Get the td that has it's text equal to urn:dece:org:org:dece:10 获取text 等于 urn:dece:org:org:dece:10td

  2. Get that td 's parent tr 获取该td的父tr

  3. From that parent tr , get the input that has an id equal to nodeAccountOid and has a type of radio . 从父tr ,得到了input有一个id 等于 nodeAccountOid并具有typeradio

Therefore it doesn't matter where exactly the elements are, as long as the XPath locator can navigate up to the parent and back down again to the input you need. 因此,元素的确切位置无关紧要,只要XPath定位器可以向上导航至parent ,然后再次向下导航至所需的input

The problem you are facing is to locate a certain element in the web page that may occur in different positions. 您面临的问题是在网页中定位某个元素,该元素可能出现在不同的位置。 If there is another element which can be identified easily by its content, id or name and there is a static relation between this element and the one you would like to locate, then you could use xpath. 如果存在可以通过其内容,ID或名称轻松识别的另一个元素,并且该元素与您要定位的元素之间存在静态关系,则可以使用xpath。 There are also some examples for this in the selenium documentation as far as I know. 据我所知,硒文档中也有一些示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM