无法点击搜索按钮

问题描述

我无法单击以下HTML中的“搜索”按钮。我尝试使用.css,.cssContaining,.className,.xpath找到它。任何帮助将不胜感激!

<div class="ncol-xs-offset-11 ncol-md-3 saveFilter">
    <button class="searchBtn-align">
        <i class="fa fa-search fa-lg">
        </i> Search
    </button> 
    <button class="searchBtn-align">
        <i class="fa fa-refresh fa-lg">
        </i> Reset
    </button>
</div>

CODE

var buttonSearch = element(by.xpath('//button[. = " Search"]'))
browser.isElementPresent(buttonSearch).then(function (present) {
    if (present == true) {
        //buttonSearch.click();
        console.log('Search button was clicked');   
    }
    else {
        console.log('Search button was NOT located');
    }
})

解决方法

您可以按以下方式使用xpath定位器://button[contains(.,'Search')]。然后

var buttonSearch = element(by.xpath("//button[contains(.,'Search')]"))
buttonSearch.isPresent().then(function (present) {
    if (present) {
        buttonSearch.click().then(()=>{
          console.log('Search button was clicked');
        });
    } else {
        console.log('Search button was NOT located');
    }
})

或使用async

var buttonSearch = element(by.xpath("//button[contains(.,'Search')]"))

if (await buttonSearch.isPresent()) {
  await buttonSearch.click();
  console.log('Search button was clicked');   
} else {
  console.log('Search button was NOT located');
}
,

您尝试过使用element(by.css('.searchBtn-align'));吗?

甚至是element(by.buttonText('Search'));