2017-06-19 9 views
1

ページを読み込んでいる間、私の入力は 'readonly'属性を持っています。この属性が削除されているかどうかを確認する方法私はC#入力readonly属性がなくなるのを待つ

私のコードでセレンを使用しています:

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

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

このコードは動作しますが、私はこれを行うために、より適切な方法があると思います。

答えて

1

あなたのinputReadOnly変数を削除するよりも、このコードを改善する方法は他にありません。 あなたは、このことによってあなたのwhileループを置き換えることができますどこにもそれを使用しない場合:

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

は、この情報がお役に立てば幸いです。

0

あなたはまた、最良の方法はであるこの無限ループを回避するために

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"); 
+0

Seleniumを扱うときに 'WebDriverWait'を使用する方が良いです。 –

1

のためにあなたが待つ時間を制限する場合があります

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に変更されると、次のコード行が実行されます。

+0

あなたは何を意味するのコードを共有できますか?私が知っているように、 'ExpectedConditions'は' AttributeToBe'の定義を含んでいません。 –

関連する問題