のためにあなたが待つ時間を制限する場合があります
IWebElement input = driver.FindElement(By.ClassName("myInput"));
while (input.GetAttribute("readonly") == "true");
input.SendKeys("Text");
ような何かを行うことにより、ラインの数を減らすことができます'wait'と呼ばれる組み込みの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;
});
}
ステップ2:使用方法
IWebElement x = driver.FindElement(By.ClassName("myInput")) // Initialization
x.WaitUntilAttributeValueEquals("readonly",null)
input.SendKeys("Text");
説明:このコードは、 "readonly
" 属性かどうか、20秒の間に(これは '待機' メソッドのデフォルトの動作です)500msごとにチェックします指定されたIWebElement
の値はnullです。 20秒後にまだnull
でない場合、例外がスローされます。値がnull
に変更されると、次のコード行が実行されます。
Seleniumを扱うときに 'WebDriverWait'を使用する方が良いです。 –