2016-07-28 112 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的存在決定了狀態,但它不能保證元素被實現的樣式禁用。

相關問題