【问题标题】:Get selected checkbox without Name or Xpath in selenium webdriver在 selenium webdriver 中获取没有名称或 Xpath 的选中复选框
【发布时间】:2016-03-21 10:47:14
【问题描述】:

如何从没有名称或 xpath 的复选框列表中获取选定的复选框

复选框图片enter image description here

生成的 HTML 代码

<div class="checkbox ng-scope" ng-repeat="item in options track by $index">
   <label class="ng-binding" style="float:left;">
<input type="checkbox" class="ng-pristine ng-valid" ng-change="textfieldKeypress(onkeypress)" ng-model="$parent.inputArray[$index]" validator="[onwatch,onblur]" value="item" >
    Ache </label> 
</div>

这是我尝试过但不是正确的方法,因为有多个没有名称的复选框列表

List<WebElement> CheckBoxList = driver.findElements(By.tagName("checkbox"));
    for (int i = 0; i < CheckBoxList.size(); i++) {
        if (CheckBoxList.get(i).isSelected()) {
            Assert.assertEquals("Ache", CheckBoxList.get(i).getText());
        }
    }

【问题讨论】:

  • 您是否尝试过 byModel:angular.github.io/protractor/#/…
  • @maurycy 我正在使用 Selenium Webdriver。不是量角器..
  • 为什么不想使用 xpath?
  • 在您的示例中,所有复选框都有名称,如果复选框没有名称,您究竟想显示什么。
  • 我看不到“复选框”这样的标签,你的代码应该是List&lt;WebElement&gt; CheckBoxList = driver.findElements(By.tagName("input"));

标签: angularjs selenium xpath selenium-webdriver


【解决方案1】:

不要使用isSelected(),而是使用.getAttribute("checked"),然后在if子句中将返回值与布尔值true进行比较。

【讨论】:

    【解决方案2】:

    试试下面的xpath = //div[@class='checkbox ng-scope']

    如果您有多个复选框,请在下方使用xpath

    (//div[@class='checkbox ng-scope']) [1] 你把 1 的值改成 2/3/4 等等。

    【讨论】:

    • div[@class='checkbox ng-scope'] 将是手动过程,有超过 100 个具有相同 div 的复选框
    【解决方案3】:

    由于无法使用 XPath 评估复选框状态并且无法使用 CSS 选择器评估文本,因此我将采用两者中存在的元素:

    // get the checked checkboxes
    List<WebElement> m_checked = driver.findElements(By.cssSelector("input:checked[type=checkbox]"));
    
    // get the checkboxes matching the text
    List<WebElement> m_text = driver.findElements(By.xpath("//label[normalize-space(.)='Ache']/input[@type='checkbox']"));
    
    // get the checkboxes checked matching the text
    Collection elements = CollectionUtils.retainAll(m_checked, m_text);
    

    【讨论】:

      猜你喜欢
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多