Selenium Webdriver等待来自另一个元素的元素

标签 selenium selenium-webdriver webdriver automated-tests ui-automation

我正在尝试使用 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/

相关文章:

macos - Python 2.7 - 使用 Xvfb 进行 Selenium headless 测试不起作用

Selenium webdriver - click() 成功但实际上并没有发生

java - 如何迭代 cucumber 的特征文件

java - Selenium WebDriver:Java:Mac OS X:如何从CuteWebUI for ASP.NET控制CuteWebUI_Uploader_Resource的自动化文件上传

python - 如何在 Python 中将弹出窗口置于前台

selenium - 如何在 Selenium 中找到元素内的元素

python - selenium IDE 命令 'openWindow' 是否有等效的 selenium python webdriver 命令?

java - 如何通过 Selenium-WebDriver 正确查找元素以用于输入目的

Java Selenium 3.3.1 和 geckodriver 0.15.0 不在 Windows 10 上等待

python - 从日期选择器获取网站 x 个月的可用性