简体   繁体   English

使用Selenium CSS选择器获取单选按钮的ID

[英]Get ID of a radio button with Selenium CSS selector

I just wanted to know that how can I get the ID of 我只想知道我怎样才能获得ID

<input type="radio" class="class1" id="radio1" />

with the help of CSS selectors in Selenium. 在Selenium的CSS选择器的帮助下。

This is a more difficult and intriguing question than you might suppose. 这是一个比你想象的更困难和有趣的问题。 It is important to be aware that values for the HTML class attribute are not unique . 请务必注意,HTML class属性的值不是唯一的 Thus, it is quite possible (and in fact probable) that your HTML page may have multiple elements with the class set to "class1". 因此,很可能(实际上可能)您的HTML页面可能有多个元素,类设置为“class1”。 So selecting strictly on the class will only work if your radio button is the first element on your web page with that class value. 因此,只有当您的单选按钮是具有该类值的网页上的第一个元素时,才能严格选择该类。 Working only with your example, a safer locator expression would be 仅使用您的示例,将有一个更安全的定位器表达式

selenium.GetAttribute("css=input.class1[type='radio']@id");

This matches only class1 elements that are radio buttons. 这仅匹配作为单选按钮的class1元素。 It is much more specific--it correctly avoids prior elements that happen to also have the class1 value for the class attribute. 它更加具体 - 它正确地避免了碰巧也具有class属性的class1值的先前元素。 But it is still not satisfactory--again it will match the first such element. 但它仍然不能令人满意 - 再次它将匹配第一个这样的元素。 You can improve further if, for example, you have more than one group of radio buttons. 例如,如果您有多组单选按钮,则可以进一步改进。 Say you wanted the first radio button in the second group from this code fragment... 假设您想从此代码片段中获取第二组中的第一个单选按钮...

<div id='group1'>
  <input type="radio" class="class1" id="radio1" />
  ...
</div>
<div id='group2'>
  <input type="radio" class="class1" id="radio1" />
  ...
</div>

... you could use a locator like this: ...你可以使用这样的定位器:

selenium.GetAttribute("css=#group2 .class1[type='radio']@id");

(Note that the space after '#group2' is important!) (注意'#group2'之后的空格很重要!)

However, the above is mostly to explain the problem rather than to provide a good answer, because the fundamental problem remains. 但是,上述主要是解释问题而不是提供一个好的答案,因为根本问题仍然存在。

Most likely your code is really something like this, with multiple radio buttons all having the same type and same class, distinguishable only by position: 很可能你的代码真的是这样的,多个单选按钮都具有相同的类型和相同的类,只能通过位置区分:

<input type="radio" class="class1" id="radio1" />
<input type="radio" class="class1" id="radio2" />
<input type="radio" class="class1" id="radio3" />

The only way to target one of these other than the first is by explicit indexing, but that requires you know the index a priori , eg: 除了第一个之外,唯一的方法是通过显式索引来定位其中一个, 但这需要您先验地知道索引 ,例如:

selenium.GetAttribute("css=#group2 [type='radio']:nth-child(2)@id");

(For more on constructing CSS recipes for Selenium see my article and wallchart XPath, CSS, DOM and Selenium: The Rosetta Stone on Simple-Talk.com.) (有关为Selenium构建CSS配方的更多信息,请参阅我的文章和挂图XPath,CSS,DOM和Selenium: Simple-Talk.com上的Rosetta Stone 。)

I will give you an example: 我会举个例子:

selenium.GetAttribute("css=.class1@id");

Here is the link for the documentation http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.DefaultSelenium.GetAttribute.html 以下是文档的链接http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.DefaultSelenium.GetAttribute.html

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

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