C# 硒等待不';不要等待元素被点击
我有一个动态加载的页面,其中包含一个按钮。我正在尝试等待按钮可用,以便使用C#绑定通过selenium单击。我有以下代码:C# 硒等待不';不要等待元素被点击,c#,selenium,wait,C#,Selenium,Wait,我有一个动态加载的页面,其中包含一个按钮。我正在尝试等待按钮可用,以便使用C#绑定通过selenium单击。我有以下代码: WebDriverWait wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(30)); wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("addInspectionButton"))); var b
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();
但这不起作用。单击事件从未触发。selenium脚本不会引发异常,警告ID为“addInspectionButton”的元素不存在。它只是无法单击它。如果我在wait语句和获取button元素句柄的行之间添加一个Thread.Sleep(3000),它就会工作
我是否在此处未正确使用ExpectedConditions.Element可单击?在按钮被动态添加到页面后,事件被绑定到按钮。所以按钮被点击了,但什么也没发生。放置在代码中的睡眠线程只是给客户端事件绑定时间 我的解决方案是单击按钮,检查预期结果,然后如果预期结果还不在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
}
按钮。单击()//单击按钮以打开窗体
var forms=Driver.Instance.FindElements(By.Id(“inspectionDetailsForm”)//查询表单的DOM
var时间=0//记录按钮被点击的次数
while(forms.Count<1&×<100)//如果表单尚未加载,请重新单击按钮并在DOM中检查表单,仅重试100次
{
按钮。单击();//重新单击按钮
forms=Driver.Instance.FindElements(By.Id(“inspectionDetailsForm”);//重新查询表单的DOM
times++;//跟踪单击的时间
}
看起来您正在正确使用它。。。您是否尝试过单击wait.until调用返回的web元素?i、 e.var button=wait.Until(ExpectedConditions.elementtobelickable(By.Id(“addInspectionButton”));我试过了,我也有同样的行为。如果我抛出一条线,在我得到按钮手柄的地方和我实际点击它的地方之间睡觉,奇怪的是它确实起作用。不管是哪种方式,它就像等待实际上并不等待按钮被点击,我想我已经发现这个按钮有一个动态绑定的事件。当页面加载并单击按钮时,事件尚未绑定。这就是睡眠起作用的原因。据我所知,selenium中没有等待客户端事件的方法。您如何设置事件?听起来,如果您使用更经典的button.onclick=this\u thing
方法,您可能会自己等待,通过jsI验证该变量。在尝试自动化NetSuite时,也会遇到同样的问题