如何只点击未选中的webdriver Java按钮

时间:2015-04-13 16:22:40

标签: java list selenium-webdriver click

这是我到目前为止,尝试使其仅点击未选择的元素,但它也点击选定的元素。似乎忽略了if语句。

WebElement parent = driver.findElement(By.cssSelector(".tutor-schedule-tablet"));  
List<WebElement> children = parent.findElements(By.cssSelector("button.md-primary.md-button.ng-scope"));  


  for (int i = 0; i < children.size(); i++) { 
      if(!children.get(i).isSelected())
      {
          children.get(i).click();
      }
  }  

点击

<button tabindex="0" class="md-primary md-button ng-scope timeinterval-selected" ng-class="{ 'timeinterval-selected' : weekinterval.timeIntervals[sectionIndex].selected, 'timeinterval-select-disabled' : weekinterval.timeIntervals[sectionIndex].isPastDate }" ng-click="itemClicked_Weekly(weekinterval.timeIntervals[sectionIndex])" ng-if="!weekinterval.timeIntervals[sectionIndex].disabled &amp;&amp; !weekinterval.timeIntervals[sectionIndex].isPastDate">
                    <span class="ng-binding ng-scope">&nbsp;</span>
                    <div class="md-ripple-container"></div>
                    </button>

这是未被点击的

<button tabindex="0" class="md-primary md-button ng-scope" ng-class="{ 'timeinterval-selected' : weekinterval.timeIntervals[sectionIndex].selected, 'timeinterval-select-disabled' : weekinterval.timeIntervals[sectionIndex].isPastDate }" ng-click="itemClicked_Weekly(weekinterval.timeIntervals[sectionIndex])" ng-if="!weekinterval.timeIntervals[sectionIndex].disabled &amp;&amp; !weekinterval.timeIntervals[sectionIndex].isPastDate">
                    <span class="ng-binding ng-scope">&nbsp;</span>
                    <div class="md-ripple-container"></div>
                    </button>

这是HTML的其余部分

<div class="tutor-schedule-tablet" hide-sm="">

    <h3></h3>
    <div class="schedule-day-navigation md-toolbar-tools" layout="row"></div>
    <table class="tutor-schedule tutor-schedule-week" cellspacing="1">
        <thead></thead>
        <tbody>
            <!--

             ngRepeat: timeinterval in weekintervals[0].timeIn…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals"></tr>
            <!--

             end ngRepeat: timeinterval in weekintervals[0].ti…

            -->
            <tr class="ng-scope" ng-init="sectionIndex = $index" ng-repeat="timeinterval in weekintervals[0].timeIntervals">
                <td id="timeintrval_4pm" class="timeinterval-name text-center ng-binding" scroll-if="timeinterval.name == '12pm'"></td>
                <!--

                 ngRepeat: weekinterval in weekintervals 

                -->
                <td class="ng-scope" ng-repeat="weekinterval in weekintervals">
                    <!--

                    {{weekinterval.timeIntervals[$parent.$index].name}…

                    -->
                    <!--

                     ngIf: weekinterval.timeIntervals[sectionIndex].di…

                    -->
                    <!--

                     ngIf: !weekinterval.timeIntervals[sectionIndex].d…

                    -->
                    <!--

                     ngIf: !weekinterval.timeIntervals[sectionIndex].d…

                    -->
                    <button class="md-primary md-button ng-scope timeinterval-selected" ng-if="!weekinterval.timeIntervals[sectionIndex].disabled && !weekinterval.timeIntervals[sectionIndex].isPastDate" ng-click="itemClicked_Weekly(weekinterval.timeIntervals[sectionIndex])" ng-class="{ 'timeinterval-selected' : weekinterval.timeIntervals[secti…led' : weekinterval.timeIntervals[sectionIndex].isPastDate }" tabindex="0"></button>

<!--

 end ngIf: !weekinterval.timeIntervals[sectionInde…

-->

1 个答案:

答案 0 :(得分:2)

看着它。可能Selenium不知道它是否被选中。当选择元素时,我看到class属性发生了变化。所以,这可能是你最好的选择

WebElement parent = driver.findElement(By.cssSelector(".tutor-schedule-tablet"));  
List<WebElement> children = parent.findElements(By.cssSelector("button.md-primary.md-button.ng-scope"));  


  for (int i = 0; i < children.size(); i++) { 
      if(!children.get(i).getAttribute("class").contains("timeinterval-selected"))
      {
          children.get(i).click();
      }
  } 

注意:未经过测试的代码和语法可能很少