微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 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脚本不会抛出异常,提示ID为“addinspectionButton”的元素不存在.它只是无法点击它.如果我添加一个Thread.Sleep(3000)在wait语句和我得到按钮元素句柄的行之间它可以工作.

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

解决方法

事实证明,在将按钮动态添加页面后,事件被绑定到按钮.所以按钮被点击但没有发生任何事情.放置在代码中的睡眠线程只是给客户端事件时间绑定.

我的解决方案是单击按钮,检查预期结果,然后重复,如果预期结果尚未在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
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐