簡體   English   中英

Selenium C#:斷言文本字段是否不可編輯

[英]Selenium C#: Assert verifying if text field is uneditable

我正在嘗試編寫腳本來斷言所討論的文本字段是否不可編輯。 我試圖寫一個隱藏的驗證=“ true”,因為這最終是隱藏可編輯的文本框。 我將如何驗證這樣的事情? 這是我應該進行這種驗證的方式嗎? 感謝您的提前答復。

這是HTML

<div id="ctl00_contentMain_sponsorInfo">
<fieldset>
    <legend>
        Referral Information
    </legend>
    <ol>
        <li>
            The name of your referrer is Name…
        </li>
        <li>
            <input id="ctl00_contentMain_sponsorUsername" class="textBig" type="text" hidden="true" style="width:270px;" req="false" max="30" value="Name" name="ctl00$contentMain$sponsorUsername"></input>
        </li>
    </ol>
</fieldset>
</div>

正如您提到的,測試此方案的最佳方法是查看hidden attribute是否具有值true

IWebElement element = Driver.FindElement(By.Name("ctl00$contentMain$sponsorUsername"));
if (element.GetAttribute("hidden").Contains("true"))
{
    Assert.Pass("Your message");
}

注意:您必須確保該元素存在。 您可以使用一些類似的顯式等待

 var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.ElementExists(By.CssSelector("something "))); 

為了獲取屬性。 這種情況下,Selenium可以正常工作,因為您沒有與該元素進行交互。

WebDriver僅與可見元素交互。 您可以使用JavaScriptExecutor

 IJavaScriptExecutor js = driver as IJavaScriptExecutor;
 string hiddenStatus = (string)js.ExecuteScript("return document.getElementById('ctl00_contentMain_sponsorUsername').hidden");

hiddenStatus將為true / false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM