如何使用 C# 在 selenium webdriver 中单击网页上的“mailto:”链接时验证新电子邮件窗口是否打开

问题描述 投票:0回答:1

我已尝试以下方法来验证单击网页上的 mailto 链接后是否生成了新的窗口句柄,但网络驱动程序似乎只能识别一个窗口句柄,即网页本身。我下面的代码应该验证单击 mailto 链接后是否打开了新的电子邮件窗口,但它不起作用。

由于它只能识别一个窗口句柄,因此无法切换到邮件客户端。

    internal void SendNewEmail()
    {
        _elementInteraction.Click(_emailObject);

        Thread.Sleep(5000); 

        string mainWindowHandle = driver.CurrentWindowHandle;
        // Get all window handles after the click
        var windowHandles = driver.WindowHandles;

        // Iterate through the window handles
        foreach (var handle in windowHandles)
        {
            // Switch to the new window
            if (handle != mainWindowHandle)
            {
                driver.SwitchTo().Window(handle);

                // Check if the title of the new window contains expected text (modify as needed)
                if (driver.Title.Contains("New Email"))
                {
                    Console.WriteLine("New email window opened successfully.");
                    // Close the new window (optional)
                    driver.Close();
                }
                else
                {
                    Console.WriteLine("New email window not opened.");
                }

                // Switch back to the main window
                driver.SwitchTo().Window(mainWindowHandle);
            }
        }

        // Close the WebDriver
        driver.Quit();
    }

一如既往,非常感谢任何帮助。谢谢

编辑:没有错误消息,即使打开新电子邮件后,它也仅将网页识别为窗口句柄。因此它无法“切换”到新窗口。抱歉我无法提供更多信息。

c# selenium-webdriver mailto
1个回答
0
投票

Selenium Web 驱动程序只能与 Web 浏览器窗口交互。邮件客户端是一个单独的应用程序,因此 Selenium 无法与其交互。使用 Selenium 无法实现您想要完成的任务。

© www.soinside.com 2019 - 2024. All rights reserved.