Angularjs:识别Selenium中的按钮

时间:2016-04-26 07:36:03

标签: angularjs selenium-webdriver robotframework

我试图识别angularjs中的按钮。 HTML代码位于下方。如何使用硒来区分这些按钮。我使用机器人框架进行自动化,但只要有人能够使用css选择器或xpath或任何其他有助于识别它们的方法帮助识别这些元素/按钮,这并不重要。

我有4个按钮 先生 太太 女士 其他

Html for 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>

3 个答案:

答案 0 :(得分:1)

您可以通过文字识别按钮中的<span>,然后点击它

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

答案 1 :(得分:0)

RobotFramework查找其中一个按钮的方法是

Click Button  xpath=//span[text()="Mr"]/parent::button

答案 2 :(得分:-1)

// 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