【发布时间】:2020-02-25 22:00:14
【问题描述】:
我正在使用带有 NUnit 和 Chrome 浏览器的 Visual Studio 中的 c# 编写一些 selenium 自动化。在网页中有一个是按钮,它会打开一个新窗口。
网页状态
您要继续是/否吗?
回答 [Yes] 会打开一个新窗口,然后在其中使用 [Print] 按钮打开另一个新窗口
当在 Selenium 中单击 [Yes] 按钮时,新窗口打开但单击事件未完成,代码在单击时挂起,然后在 60 秒后引发超时错误。 为了解决这个问题,我将单击包含在 try catch 中以处理错误。但是,下一段代码也会在 60 秒后超时。
很有可能我让这段代码在调试中的 try-catch 上运行,发现如果我睡了 9 分钟(总共 10 分钟,包括 60 秒的点击超时),那么代码会继续运行,不会有任何进一步的超时.如果我减少睡眠时间,超时失败会再次发生。
try
{
//This click works but doesnt complete, and then throws a timeout error after 60 seconds and falls into the catch
DialogYesBtn.Click();
}
catch
{
Thread.Sleep(540000); //9 minutes if i drop this to 8 minutes then the next step also times out
}
// this works with the above timeouts - if i dont sleep in the catch this times out after 60 seconds as well
SeleniumDriver.Instance.SwitchTo().Window(SeleniumDriver.Instance.WindowHandles.Last());
//unable to find this element using POM
PrintBtn.Click();
知道这里发生了什么吗?我需要在最后一个窗口中单击一个按钮,但 Selenium 似乎找不到它们。我还需要一个有效的解决方案来解决当前编写的代码,然后等待 10 分钟以等待某种超时发生。
我在 PrintBtn 单击之前将以下调试代码部分放在循环每个窗口以查看哪些元素可用但在任何窗口中都找不到。
//finds 3 windows, the original and the 2 that opened onclick
List<string> windows = SeleniumDriver.Instance.WindowHandles.ToList();
foreach (var handle in windows)
{
SeleniumDriver.Instance.SwitchTo().Window(handle);
//none of these elements are found in any window
var elems1 = SeleniumDriver.Instance.FindElements(By.Id("header"));
var elems2 = SeleniumDriver.Instance.FindElements(By.Id("button-strip"));
var elems3 = SeleniumDriver.Instance.FindElements(By.TagName("control-button"));
var elems4 = SeleniumDriver.Instance.FindElements(By.ClassName("action-button"));
}
【问题讨论】:
-
我猜在您的机器/公司中单击网页时打开的链接没有加载或被阻止?
-
不,我不这么认为?当我运行调试循环时: SeleniumDriver.Instance.SwitchTo().Window(handle) ...新窗口在切换到时清晰地成为焦点,
-
没关系。我在问页面加载是否完成或标签中的
loading图标一直在旋转?如果页面加载的某些网站(例如:facebook 小部件或 youtube 小部件)由于公司政策而被阻止,加载将仅在超时后停止。 -
谢谢,不,我看不到任何提示加载尚未完成,没有加载图标,也没有旋转。单击打印按钮时,新窗口关闭并返回主窗口,事务继续到下一页。
标签: c# selenium selenium-webdriver window-handles