只是想补充一点IAlert alert = driver.SwitchTo().Alert();
,至少在 InternetExplorerWebDriver 中不起作用,这可能是因为 js cofirm alert 一旦启动就会阻止一切,你甚至无法在 c# 中执行下一行代码,这很奇怪。
我必须做的是在一个单独的线程中实际执行确认弹出窗口以释放 c# 后端中的控制,以便可以执行下一行代码,并且还必须在发送密钥之前和之前执行 Thread.Sleep(1000) make确定 js 仍然没有阻塞
所以我的代码看起来像这样:
public class MyThread
{
public IWebDriver driver;
public NgWebElement element;
public MyThread(IWebDriver _driver, NgWebElement _el)
{
driver = _driver;
element= _el;
}
public void RunMe()
{
AsyncJavaScriptExecutor asyncJavaScriptExecutor = new AsyncJavaScriptExecutor(driver as IJavaScriptExecutor);
asyncJavaScriptExecutor.ExecuteScript("arguments[0].click(); callback();", new object[] { element });
}
}
然后在测试或页面对象模型中
MyThread mthread = new MyThread(_driver, element);
Thread oThread = new Thread(new ThreadStart(mthread.RunMe));
oThread.Start();
while (!oThread.IsAlive){}
//need to sleep beore and afer sending keys to makes sure js is not blicking events
Thread.Sleep(1000);
oThread.Abort();
SendKeys.SendWait("{ENTER}");
Thread.Sleep(1000);