2016-11-24 18 views
1

C#からSeleniumでログインフォームを送信しようとしています。しかし、新しいページが読み込まれるのを待つために、送信後に待機させることはできません。働いているのはThread.Sleepだけです。それを待たせるために私は何をすべきですか?ウェブサイトがロードされるまで、Seleniumはサブミッションを待っていません

[TestFixture] 
public class SeleniumTests 
{ 
    private IWebDriver _driver; 

    [SetUp] 
    public void SetUpWebDriver() 
    { 
     _driver = new FirefoxDriver(); 

     // These doesn't work 
     //_driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10)); 
     //_driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); 
    } 

    [Test] 
    public void SubmitTest() 
    { 
     _driver.Url = "http://mypage.com"; 

     _driver.FindElement(By.Name("username")).SendKeys("myname"); 
     _driver.FindElement(By.Name("password")).SendKeys("myeasypassword"); 
     _driver.FindElement(By.TagName("form")).Submit(); 

     // It should wait here until new page is loaded but it doesn't 

     // So far this is only way it has waited and then test passes 
     //Thread.Sleep(5000); 

     var body = _driver.FindElement(By.TagName("body")); 
     StringAssert.StartsWith("Text in new page", body.Text); 
    } 
} 
+1

それは 'geckodriver'の既知の問題です。 https://github.com/mozilla/geckodriver/issues/308 –

+0

'Thread.Sleep()'がうまくいきませんでしたか? Implicit/Explicitの待ち時間は、解決策を探す上で私の最初のストップとなるでしょう。 –

+0

Thread.Sleepが機能します。しかし、私はこれに対するより良い解決策を持ちたいと思っています。 –

答えて

1

これを行う最も良い方法は、最初のページの要素が古くなるのを待ってから、新しいページの要素を待つことです。あなたが持っている可能性のある問題は、あなたが存在するすべてのページに存在するボディ要素を待っているということです。要素を待つだけの場合は、移動先のページに固有の要素を見つける必要があります。それでもbodyタグを使用したい場合は、この操作を行うことができます...

public void SubmitTest() 
{ 
    _driver.Url = "http://mypage.com"; 

    _driver.FindElement(By.Name("username")).SendKeys("myname"); 
    _driver.FindElement(By.Name("password")).SendKeys("myeasypassword"); 
    IWebElement body = _driver.FindElement(By.TagName("body")); 
    _driver.FindElement(By.TagName("form")).Submit(); 

    body = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.TagName("body"))) 
    StringAssert.StartsWith("Text in new page", body.Text); 
} 
0

回答はJeffCの答えに実質的だった:私が見つけた

これを行うための最善の方法は、待つことです最初のページの要素が古くなったら、新しいページの要素を待ちます。

私はこの答えでこれを解決:https://stackoverflow.com/a/15142611/5819671

私は新しいページからbody要素を読む前に次のコードを入れて、今では動作します:

new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.Id("idThatExistsInNewPage")))); 
関連する問題