C# 硒不';t在提交后等待网站加载
我正在尝试从C#提交一个带有Selenium的登录表单。但我不能让它在提交后等待新页面加载。唯一起作用的就是睡眠。我该怎么做才能让它等待C# 硒不';t在提交后等待网站加载,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我正在尝试从C#提交一个带有Selenium的登录表单。但我不能让它在提交后等待新页面加载。唯一起作用的就是睡眠。我该怎么做才能让它等待 [TestFixture] public class SeleniumTests { private IWebDriver _driver; [SetUp] public void SetUpWebDriver() { _driver = new FirefoxDriver(); // The
[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);
}
}
我发现最好的方法是等待第一页上的元素过时,然后等待新页上的元素。您可能遇到的问题是,您正在等待body元素。。。这将出现在每一页上。如果您只想等待一个元素,那么应该找到一个对于您要导航到的页面来说是唯一的元素。如果你仍然想使用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);
}
答案实际上是杰夫克的答案:
我发现最好的方法是等待第一页上的元素过时,然后等待新页上的元素 我用这个答案解决了这个问题: 在从新页面读取body元素之前,我编写了以下代码,现在它可以工作了:
new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.Id("idThatExistsInNewPage"))));
这是
geckodriver
的一个已知问题:如果Thread.Sleep()
不起作用,您还尝试了什么?隐式/显式等待将是我寻找解决方案的第一站。Thread.Sleep有效。但我希望有更好的解决办法。