[英]Selenium C# Pop-up handling
I need to fetch text from pop up window using Selenium and C#.我需要使用 Selenium 和 C# 从弹出窗口中获取文本。
A similar example can be found at-.一个类似的例子可以在-找到。 https://demoqa.com/browser-windows - click on New window Message button. https://demoqa.com/browser-windows - 单击新窗口消息按钮。 I need get message from the opened window.我需要从打开的窗口中获取消息。 ( Note : I am not using Robot framework) I tried with Alert class, Javascript and WindowHandles but nothing worked. (注意:我没有使用 Robot 框架)我尝试使用 Alert 类、Javascript 和 WindowHandles,但没有任何效果。
WindowHandles Try: WindowHandles 尝试:
foreach (var windowHandle in Webdriver.Driver.WindowHandles)
{
if (!windowHandle.Equals(Webdriver.Driver.CurrentWindowHandle))
{
Webdriver.Driver.SwitchTo().Window(windowHandle);
break;
}
}
Javascript try: Javascript 尝试:
String javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
executor.ExecuteScript(javaScript, webElement);
You can use the below snippet to check if you are switching to the correct window to troubleshoot this.您可以使用以下代码段来检查您是否正在切换到正确的窗口来解决此问题。
var availableWindows = new List<string>(driver.WindowHandles);
foreach (string w in availableWindows)
{
driver.SwitchTo().Window(w);
if (driver.Title == "<NEW WINDOW TITLE HERE>")
{
Console.WriteLine("SWITCHED TO CORRECT WINDOW!!!");
Console.WriteLine(driver.Title);
}
else
{
Console.WriteLine("NOT THE CORRECT WINDOW !!");
Console.WriteLine(driver.Title);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.