简体   繁体   English

Selenium C# 弹出窗口处理

[英]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.

相关问题 c# Selenium:无法切换到模态/弹出窗口/iFrame - c# Selenium: Unable to SwitchTo Modal/Pop-up/iFrame 在C#中创建弹出式表单 - Creating a Pop-Up form in C# 我在C#中使用Selenium Webdriver,无法在证书弹出窗口中单击“确定” - I am using selenium webdriver in C#, I am unable to click ok in the certificate pop-up 如何使用C#中的硒自动在webrtc中单击“允许/阻止”按钮以弹出麦克风权限? - How to automate the clicking of 'Allow/Block' button in webrtc for microphone permission pop-up using selenium in C#? Selenium C# 禁用 Microsoft Chromium Edge 浏览器同步弹出窗口 - Selenium C# to disable Microsoft Chromium Edge browser Sync pop-up OpenFileDialog&SaveFileDialog在C#中带有过滤器的弹出式搜索 - OpenFileDialog & SaveFileDialog Pop-up search with filter in C# C#ADAL AcquireTokenAsync()没有弹出框 - C# ADAL AcquireTokenAsync() without pop-up box C# - 以编程方式在浏览器中打开弹出窗口 - C# - Programmatically Opening a Pop-Up Window in Browser 如何使用 C# 处理 Outlook 中的安全弹出窗口 - How to handle security pop-up in outlook using C# 在C#中以编程方式登录弹出式身份验证 - programmatically login pop-up authentication in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM