C# 等待输入只读属性消失

C# 等待输入只读属性消失,c#,selenium,C#,Selenium,加载页面时,我的输入具有“只读”属性。如何检查此属性是否已被删除?我用硒和C# 我的代码: IWebElement input = driver.FindElement(By.ClassName("myInput")); string inputReadOnly = input.GetAttribute("readonly"); while (inputReadOnly == "true") { inputReadOnly = input.GetAttr

加载页面时,我的输入具有“只读”属性。如何检查此属性是否已被删除?我用硒和C#

我的代码:

IWebElement input = driver.FindElement(By.ClassName("myInput"));
string inputReadOnly = input.GetAttribute("readonly");

while (inputReadOnly == "true")
        {
            inputReadOnly = input.GetAttribute("readonly");
        }
input.SendKeys("Text");

这段代码可以工作,但我认为有更合适的方法来实现这一点。

我看不到任何其他方法比去掉inputReadOnly变量更好。 如果您不在其他任何地方使用它,您可以用以下内容替换while循环:

while (input.GetAttribute("readonly") == "true")
{
    // maybe do a thread.sleep(n_milliseconds);
}

希望这有帮助。

您可以通过执行以下操作来减少行数

IWebElement input = driver.FindElement(By.ClassName("myInput"));
while (input.GetAttribute("readonly") == "true");
input.SendKeys("Text");
您可能还希望限制等待此操作的时间,以避免无限循环

IWebElement input = driver.FindElement(By.ClassName("myInput"));

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

while (input.GetAttribute("readonly") == "true" && stopwatch.Elapsed.TotalSeconds < timeoutInSeconds);

input.SendKeys("Text");
IWebElement input=driver.FindElement(By.ClassName(“myInput”);
秒表秒表=新秒表();
秒表。开始();
while(input.GetAttribute(“readonly”)==“true”&&stopwatch.appeased.TotalSeconds
最好的方法是使用名为“等待”的内置Selenium功能。我使用这段代码已经6个多月了,没有任何问题

步骤1:创建扩展方法

 private static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
 public static void WaitUntilAttributeValueEquals(this IWebElement webElement, String attributeName, String attributeValue)
    {           
            wait.Until<IWebElement>((d) =>
            {
                //var x = webElement.GetAttribute(attributeName); //for debugging only
                if (webElement.GetAttribute(attributeName) == attributeValue) 
                {
                    return webElement;
                }
                return null;
            });
        }

说明:此代码将在20秒内每隔500毫秒检查一次(这是“等待”方法的默认行为),检查指定的
IWebElement
的“
readonly
”属性是否等于null。如果在20秒后,它仍然不是
null
,则将抛出异常。当值更改为
null
时,将执行下一个代码行。

在使用Selenium时,最好使用
WebDriverWait
。您能分享您的意思吗?据我所知,
ExpectedConditions
不包含
AttributeToBe
的定义。
IWebElement x = driver.FindElement(By.ClassName("myInput")) // Initialization
x.WaitUntilAttributeValueEquals("readonly",null)
input.SendKeys("Text");