【问题标题】:Angularjs: identifying a button in SeleniumAngularjs:识别 Selenium 中的按钮
【发布时间】:2016-08-19 21:20:05
【问题描述】:

我正在尝试识别 angularjs 中的按钮。 HTML 代码放在下面。我如何使用 selenium 区分这些按钮。我正在使用机器人框架进行自动化,但只要有人可以帮助使用 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>

【问题讨论】:

    标签: angularjs selenium selenium-webdriver robotframework


    【解决方案1】:

    您可以通过文字识别按钮中的&lt;span&gt;并点击它

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

    【讨论】:

    • 您确定了一个跨度,而不是一个按钮
    • @jim 是的,我知道。我重新提出我的答案。
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢 Jim....这有效...谢谢 heaps..有没有使用 CSS 选择器代替 xpath 的选项?
    • 您可以选择所有buttons (css=button) 然后遍历它们直到找到您需要的那个,但这是一个非常糟糕的主意。 CSS不能用于按元素文本查找
    【解决方案3】:
    // 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
    

    【讨论】:

    • ng-scope 是在 Angular 应用程序中选择内容的一种非常糟糕的方式
    猜你喜欢
    • 2020-10-22
    • 2022-01-28
    • 2019-09-21
    • 2019-09-22
    • 2021-06-25
    • 1970-01-01
    • 2022-10-04
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多