2016-07-28 110 views
4

我在页面上有以下angular.js。正在显示的项目是angular.jsLi项目。一个是灰色的,另一个是启用的。当我使用Selenium webdriver方法.isEnabled()时,灰色和启用的项目均返回“已启用”。Selenium webdriver如何获取angularjs Li元素的启用状态

第一个问题是我如何获得.isEnabled()以处理这种类型的元素? Q 第二个问题是,假设的webdriver不会做它,我需要xpath,我想我可以使用这样的:

$x("//li[@class ='ng-scope disabled' and @id='actionCUSTARD']") 
$x("//li[@class ='ng-scope' and @id='actionRHUBARB']") 

第一个返回的东西只有在给定的ID被禁用,仅当给定的Id被启用时才是第二,这可以被构建到Java方法中以检查对于给定的id该元素是被启用还是被禁用。有没有更简单的方法来做到这一点?

</li> 
<li id="actionRHUBARB" class="ng-scope" on="deriveInvokeType(action)" ng-switch="" ng-class="{'disabled': false}" ng-repeat="action in getActionList()"> 
    <!-- 
    ngSwitchWhen: LINK_DYNAMIC 
    --> 
    <!-- 
    ngSwitchWhen: NO_INVOKE_PERMISSION 
    --> 
    <!-- 
    ngSwitchDefault: 
    --> 
    <a class="ng-scope ng-binding" ng-click="doAction(action)" ng-switch-default="" href=""></a> 
</li> 
<li id="actionCUSTARD" class="ng-scope disabled" on="deriveInvokeType(action)" ng-switch="" ng-class="{'disabled': true}" ng-repeat="action in getActionList()"> 
    <!-- 
    ngSwitchWhen: LINK_DYNAMIC 
    --> 
    <!-- 
    ngSwitchWhen: NO_INVOKE_PERMISSION 
    --> 
    <!-- 
    ngSwitchDefault: 
    --> 
    <a class="ng-scope ng-binding" ng-click="doAction(action)" ng-switch-default="" href=""></a> 
</li> 

回答

0

根据你的第一个问题,你可以使用By.id简单地得到元素,如下检查isEnabled(): -

WebElement el = driver.findElement(By.id("actionRHUBARB")) 

WebElement el = driver.findElement(By.id("actionCUSTARD")) 

并为您启用为: -

if(el.isEnabled()) { 
    //do your stuff 
} 

注意: - findElement总是给你元素,如果它存在于DOM否则会抛出NoSuchElementException

现在根据你的第二个问题,如果硒未能发现enabled元素,你可以按照以下使用By.cssSelector: -

WebElement el = driver.findElement(By.cssSelector(":not(.disabled)[id = 'actionRHUBARB']")); 
//it will find enabled element with id actionRHUBARB 

WebElement el = driver.findElement(By.cssSelector(":not(.disabled)[id = 'actionCUSTARD']")); 
//it will throw NoSuchElementException because it is not enabled 

希望它能帮助... :)

1

元素,也许是与风格pointer-events设置禁用none这是不是合作通过.isEnabled()。如果是这种情况,你可以评估由.getCssValue返回CSS值:

boolean isEnabled = !"none".equals(element.getCssValue("pointer-events")); 

还是有一段JavaScript代码:

boolean isEnabled = (boolean)((JavascriptExecutor)driver).executeScript(
    "return window.getComputedStyle(arguments[0], null).getPropertyValue('pointer-events') === 'none'" 
    , element); 

您还可以通过检查类disable的存在决定了状态,但它不能保证元素被实现的样式禁用。

相关问题