繁体   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