【发布时间】:2017-11-06 19:33:45
【问题描述】:
我正在尝试使用 Selenium WebDriverWait 类型来等待页面完全加载,然后再检查元素是否存在。我尝试了两种不同的方式。
第一个方法使用IgnoreExceptionTypes,然后在Until 方法中调用FindElement。这会立即抛出 NoSuchElementException 而无需等待。我希望这会继续尝试查找元素,直到超时,同时忽略 NoSuchElementException。它似乎不以这种方式工作。为什么方法 1 不起作用?
第二种方法使用 ExpectedConditions.ElementExists,并且似乎等待正确。
driver.Navigate().GoToUrl("http://www.google.com");
var myId = "myId";
//Method 1
var wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait1.IgnoreExceptionTypes(typeof(NoSuchElementException));
//Does not wait. Immediately throws NoSuchElementException
var result1 = wait1.Until(x => x.FindElement(By.Id(myId)));
//Method 2 - works as expected
var wait2= new WebDriverWait(driver,TimeSpan.FromSeconds(10))
.Until(ExpectedConditions.ElementExists(By.Id(myId)));
【问题讨论】:
-
你在使用页面对象模型吗?
-
我不这么认为..我的问题是除了创建驱动程序之外的所有代码
标签: c# selenium-webdriver webdriver selenium-chromedriver