c# - 在 C# 中使用 Selenium Webdriver 在按钮组类中查找按钮

标签 c# selenium selenium-webdriver webdriver

我的最终目标是能够在网页上单击"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 个结果并担心索引是否更改,我可以通过某种方式通过文本搜索结果吗?

最佳答案

最好的方法是使用 textxpath 来识别这些元素,因为它们没有任何唯一的 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/

相关文章:

c# - 如何在运行时设置 ChartArea 的 BackImage?

c# - 无法从 Win7 上的 Windows 服务发送 SMTP 电子邮件

c# - 将数据网格绑定(bind)到另一个数据网格的选定项

java - 如何在 Selenium (Java) 中的 "NoSuchElementException"之后继续执行?

java - 为什么我的机器人屏幕截图方法捕获黑屏?

c# - 用于 Unity3D 的 Visual Studio 2015 错误 "Incompatible Project"

java - 如何使用带有 Java 的 Selenium WebDriver (Selenium 2) 在文本框中键入内容?

python - 获取 URLError : <urlopen error [Errno 111] Connection refused> in selenium webdriver using python in phantomjs

java - 如何在 Selenium Webdriver 中使用 "Upload Image Button"上传文件/照片

c# - 使用 Selenium Webdriver 测试元素是否聚焦