Selenium Wait不等待Element可点击

我有一个动态加载的页面,包含一个按钮。 我正在尝试等待按钮可以使用C#绑定点击selenium。 我有以下代码:

WebDriverWait wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(30)); wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("addInspectionButton"))); var button = Driver.Instance.FindElement(By.Id("addInspectionButton")); button.Click(); 

这不起作用。 从未触发click事件。 selenium脚本不会抛出exception,提示ID为“addInspectionButton”的元素不存在。 它只是无法点击它。 如果我添加一个Thread.Sleep(3000)在wait语句和我得到按钮元素句柄的行之间它工作。

我在这里没有正确使用ExpectedConditions.ElementToBeClickable吗?

事实certificate,在将按钮动态添加到页面后,事件被绑定到按钮。 因此点击了按钮但没有发生任何事情。 放置在代码中的sleep线程只是给客户端事件时间绑定。

我的解决方案是单击按钮,检查预期结果,然后重复,如果预期结果尚未在DOM中。

由于预期的结果是打开一个表单,我像这样轮询DOM:

 button.Click();//click button to make form open var forms = Driver.Instance.FindElements(By.Id("inspectionDetailsForm"));//query the DOM for the form var times = 0;//keep tabs on how many times button has been clicked while(forms.Count < 1 && times < 100)//if the form hasn't loaded yet reclick the button and check for the form in the DOM, only try 100 times { button.Click();//reclick the button forms = Driver.Instance.FindElements(By.Id("inspectionDetailsForm"));//requery the DOM for the form times++;// keep track of times clicked }