我正在尝试使用 WebDriverWait 等待面板中的元素加载
WebDriverWait wait = WebDriverWait(MyWebDriver, 10);
By completeButton = By.Id("completeButton");
// This waits on page level and loads the first which isn't always the correct panel.
// How can I wait from a given panel?
IWebElement button = wait.Until(drv => drv.FindElement(completeButton));
button.Click();
我有多个面板,每个面板在准备就绪时都会创建完整的按钮,因此需要等待。
我能够等待整个页面上的元素,但是我希望能够等待来自另一个元素的元素。
查看文档这显然是错误的,但是我可以做类似的事情来等待子页面中的元素吗,其他人是如何解决这个问题的?
WebDriverWait wait = WebDriverWait(MyPanelSubElement, 10);
最佳答案
1.创建一个 WebElementWait 类:
public class WebElementWait : DefaultWait<IWebElement>
{
public WebElementWait(IWebElement element, TimeSpan timeout) : base(element, new SystemClock())
{
this.Timeout = timeout;
this.IgnoreExceptionTypes(typeof(NotFoundException));
}
}
2.现在将其用作:
//Find your target panel . You can use WebDriverWait to lookup this element
//while waiting on webdriver if required.
IWebElement panel = //some code to get your container panel ;
IWebElement targetButton = new WebElementWait(panel, TimeSpan.FromMilliseconds(10000)).Until(p => { return p.FindElement(By.Id("completeButton")); });
targetButton.Click();
希望这对您有所帮助!
关于Selenium Webdriver等待来自另一个元素的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51880381/