加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Selenium Wait不等待Element可点击

发布时间:2020-12-15 22:17:22 所属栏目:百科 来源:网络整理
导读:我有一个动态加载的页面,包含一个按钮.我正在尝试等待按钮可以使用C#绑定点击selenium.我有以下代码: WebDriverWait wait = new WebDriverWait(Driver.Instance,TimeSpan.FromSeconds(30)); wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("add
我有一个动态加载的页面,包含一个按钮.我正在尝试等待按钮可以使用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
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读