硒等待不等待元素是可点击 [英] Selenium Wait doesn't wait for Element to be Clickable

查看:185
本文介绍了硒等待不等待元素是可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,动态加载,并包含一个按钮。我想等到按钮可被点击与使用C#绑定硒。我有以下代码:

I have a page that is dynamically loaded and contains a button. I am trying to wait for the button to be available to be clicked with selenium using the C# bindings. I have the following code:

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事件永远不会被解雇。硒脚本不会抛出异常报警,与addInspectionButton的ID的元素不存在。它只是无法点击。如果我加入等待语句而行,我得到它的工作原理按钮元件上的手柄之间的Thread.sleep代码(3000)。

this doesn't work though. The click event is never fired. The selenium script doesn't throw an exception alerting that the element with an ID of "addInspectionButton" doesn't exist. It just isn't able to click it. If i add a Thread.Sleep(3000) Between the wait statement and the line where I get a handle on the button element it works.

我不能使用ExpectedConditions .ElementToBeClickable正确吗?

Am i not using the ExpectedConditions.ElementToBeClickable correctly here?

推荐答案

原来,事件正在绑定到该按钮后,按钮被动态添加到页。因此,被点击的按钮只不过什么也没有发生。睡眠线程被放置在该代码只是给要绑定的客户端事件的时间。

It turns out that an event was being bound to the button after the button was dynamically added to the page. So the button WAS being clicked but nothing was happening. The sleep thread being placed in the code was just giving the client side event time to be bound.

我的解决办法是按一下按钮,检查预期的结果,然后重复,如果预期的结果在DOM还没有。

My solution was to click the button, check for the expected result, and then repeat if the expected result wasn't in the DOM yet.

由于预期的结果是一种形式,打开我接受调查的DOM是这样的:

Since the expected result was for a form to open I polled the DOM like this:

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
        }

这篇关于硒等待不等待元素是可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆