题:


我有8个按钮,例如(发现,新闻,人,视频,体育,技术,另类,ents)。所以我想单击任何一个或多个按钮。实际上,我不知道在这里使用什么,我在想其他方法是否可行。需要一些帮助。我觉得这里存在一个问题,所有按钮都具有相同的类名,因此,如果不是这种类型的问题,该如何使用。



<div class="btn-group">
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">discover</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">news</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">people</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">videos</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">sport</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">tech</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">offbeat</button>
<button class="btn btn-default button ng-scope ng-binding" type="button" ng-repeat="category in categories" ng-click="addTag(category.text)">ents</button>
</div>





if(!driver.findElement(By.className("default button ")).isSelected())
    {
      driver.findElement(By.className("default button")).sendKeys("discover");
    }
    else
    {
        driver.findElement(By.className("default button")).sendKeys("news");
    }



题:


我有8个标签作为下拉菜单(发现,新闻,人,视频,体育,技术,另类,条目)。所以我只选择一个标签。我该如何使用if else语句...



   <div class="input-group">
<span class="input-group-addon title">Category</span>
<input class="form-control ng-valid ng-dirty ng-valid-editable" typeahead="category.text for category in categories | filter:$viewValue | limitTo:8" ng-model="article.category" placeholder="Enter Category">
<ul class="dropdown-menu ng-isolate-scope" ng-style="{display: isOpen()&&'block' || 'none', top: position.top+'px', left: position.left+'px'}" typeahead-popup="" matches="matches" active="activeIdx" select="select(activeIdx)" query="query" position="position" style="display: none; top: 34px; left: 165.05px;"> </ul>
</div>





提前致谢 !!

最佳答案

尝试这种逻辑,它将根据您提供给方法的名称单击一个按钮。

public void clickButton(String buttonName) throws Exception{

    List<WebElement> buttons = driver.findElements(By.xpath("//div/button"));
           for(int i=0;i<buttons.size();i++){
                String buttonText= buttons.get(i).getText();
                if (buttonText.equals(buttonName)) {
                     buttons.get(i).click();
                     int flag =1;
                 }
            }

       if (!flag == 1)
        throw new Exception("The desired button is not present.");

    }

10-05 23:01
查看更多