1

無法找到複選框是否被檢查?拋出異常。如何使用Selenium中的XPath選中或取消選中複選框?

我使用這個的XPath複選框:

.//*@id='row_SelectedProductURLIds0']//input[@id='actualcheckbox_SelectedProductURLIds' and @type='checkbox'] 

圖片:

enter image description here

代碼:

public bool VerifyCheckbox(By by, String expected) 
    { 
     bool isPresence = false; 
     WaitUntilElementIsPresent(by); 
     //string value = GetText(by); 
     //bool actual = Convert.ToBoolean(value); 
     bool expect = Convert.ToBoolean(expected); 
     // actual = expect ? isPresence = true : isPresence = false; 
     isPresence = Driver.FindElement(by).Selected; 
     Assert.AreEqual(expect, isPresence); 
     if (isPresence != expect) 
     { 
      isPresence = false; 
     } 
     else 
     { 
      isPresence = true; 
     } 

     return isPresence; 

    } 
+1

你有什麼異常? –

+1

你的'xpath'似乎是錯誤的使用這個//輸入[@ id ='actualcheckbox_SelectedProductURLIds'和@ type ='checkbox']'.. –

+1

你可以使用.isSelected()方法來驗證複選框是否被選中或者不。 –

回答

0

我不知道你正在嘗試完成但我簡化了它。

public bool VerifyCheckbox(By by, bool expected) 
{ 
    bool actual = Driver.FindElement(by).Selected; 
    Assert.AreEqual(expected, actual); 
    return (expected == actual); 
} 

我不用猜我明白你爲什麼要斷言它們是否相等回報,如果他們是平等的。看起來多餘。最好擺脫這個整體功能,只做下面的事情。

Assert.AreEqual(expected, Driver.FindElement(By.Id("actualcheckbox_SelectedProductURLIds")).Selected); 
相關問題