Angularjs: 在 Selenium 中识别一个按钮

Angularjs: identifying a button in Selenium

我正在尝试识别 angularjs 中的一个按钮。 HTML 代码放在下面。我如何使用硒来区分这些按钮。我正在使用 robotframework 进行自动化,但只要有人可以使用 css 选择器或 xpath 或任何其他有助于唯一识别它们的方法帮助识别这些 elements/buttons 就没关系。

我有 4 个按钮 先生 太太 小姐 其他

Html 4 如下

<button ng-repeat="choice in question.choices" ng-class="{selected: isSelected(choice.value)}" ng-click="selectAnswer(choice)" tabindex="1" class="ng-scope"><span fittext=".8" fittext-max="16" class="ng-scope ng-binding" style="display: inline-block; white-space: nowrap; line-height: 1; font-size: 16px;">Mr</span></button>

<button ng-repeat="choice in question.choices" ng-class="{selected: isSelected(choice.value)}" ng-click="selectAnswer(choice)" tabindex="1" class="ng-scope"><span fittext=".8" fittext-max="16" class="ng-scope ng-binding" style="display: inline-block; white-space: nowrap; line-height: 1; font-size: 16px;">Mrs</span></button>

<button ng-repeat="choice in question.choices" ng-class="{selected: isSelected(choice.value)}" ng-click="selectAnswer(choice)" tabindex="1" class="ng-scope"><span fittext=".8" fittext-max="16" class="ng-scope ng-binding" style="display: inline-block; white-space: nowrap; line-height: 1; font-size: 16px;">Ms</span></button>

<button ng-repeat="choice in question.choices" ng-class="{selected: isSelected(choice.value)}" ng-click="selectAnswer(choice)" tabindex="1" class="ng-scope"><span fittext=".8" fittext-max="16" class="ng-scope ng-binding" style="display: inline-block; white-space: nowrap; line-height: 1; font-size: 16px;">Other</span></button>

您可以通过文字识别按钮中的<span>并点击

driver.findElement(By.xpath("//span[text()='Mr']"));
driver.findElement(By.xpath("//span[text()='Mrs']"));
//...

RobotFramework 找到这些按钮之一的方法是

Click Button  xpath=//span[text()="Mr"]/parent::button
// first take each button inside the list 
List<WebElement> myButton = driver.findElements(By.className("ng-scope"));
System.out.println("Size of the button the webpage is : " + myButton.size());
// now you can click button on the basis of index like below

myButton.get(0).click(); // for first button
myButton.get(1).click(); // for second button
myButton.get(2).click(); // for third button
myButton.get(3).click(); //For Forth Button