我的最终目标是能够在网页上单击"is"或“否”。
使用 fire bug 我可以看到 Yes 和 No 按钮位于一个名为“btn-group”的类中
<div class="btn-group btn-group-default answer-toggle" aria-label="..." role="group">
<button class="btn ng-pristine ng-valid active btn-secondary ng-touched" btn-radio="true" ng-model="consultation.previousSoundVoid" ng-class="{'btn-secondary': consultation.previousSoundVoid}" type="button">
Yes
</button>
<button class="btn ng-pristine ng-untouched ng-valid" btn-radio="false" ng-model="consultation.previousSoundVoid" ng-class="{'btn-secondary': consultation.previousSoundVoid == false}" type="button">
No
</button>
</div>
我想我可以通过 XPath 找到 Element,但我正在努力提高我的 Selenium 技能。
我希望有一种方法可以首先找到“btn-group”类,然后以某种方式在其中选择是或否。
或者能够为"is"和“否”选项创建变量。如果我这样做:
var button = Driver.Instance.FindElements(By.ClassName("btn"));
它返回 21 个选项。我发现:
button[15].Text returns "Yes"
button[16].Text returns "No"
不必查看所有 21 个结果并担心索引是否更改,我可以通过某种方式通过文本搜索结果吗?
最佳答案
最好的方法是使用 text
和 xpath
来识别这些元素,因为它们没有任何唯一的 id
//button[.='Yes']
迫切希望使用 cssSelector
吗?
.btn.ng-pristine.ng-valid.active.btn-secondary.ng-touched
因为它是复合类
你也可以用 css 去 nth-child()
[role='group']>button:nth-child(1)
而 2
将允许您选择带有文本 No
关于c# - 在 C# 中使用 Selenium Webdriver 在按钮组类中查找按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29381152/