selenium-webdrivermicrosoft-edge-extensionselenium-edgedriver

Selenium WebDriver Failing to Navigate to URL in New Tab Body


I'm having trouble with my Selenium WebDriver project in VB.NET. I'm trying to open a new tab in Edge and navigate to a specific URL, but the tab remains blank. Here's the relevant code snippet:

Imports OpenQA.Selenium
Imports OpenQA.Selenium.Edge
Imports OpenQA.Selenium.Support.UI
Imports System.Threading

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' Setup EdgeOptions
        Dim options As New EdgeOptions()

        options.AddArgument("--user-data-dir=C:\Users\jasonfan\AppData\Local\Microsoft\Edge\User Data")
        options.AddArgument("--remote-debugging-pipe")
        options.AddArgument("--allow-running-insecure-content")
        options.AddArgument("log-level=3")

        Dim driver As IWebDriver = New EdgeDriver(options)

        Try
            driver.Navigate().GoToUrl("http://www.bing.com")

            ' Open a new tab
            Dim jsExecutor As IJavaScriptExecutor = CType(driver, IJavaScriptExecutor)
            jsExecutor.ExecuteScript("window.open('about:blank', '_blank');")
            Dim newTabHandle As String = driver.WindowHandles.Last
            driver.SwitchTo().Window(newTabHandle)

            ' Navigate to the new URL
            driver.Navigate().GoToUrl("https://www.google.com/")

            MessageBox.Show("Process completed.")

        Catch ex As WebDriverException
            MessageBox.Show("Error: " & ex.Message)
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message)
        Finally
            ' Clean up
            driver.Quit()
        End Try
    End Sub

End Class

I've ensured that both the WebDriver and Edge browser are up to date. Is there any additional configuration or setting I might be missing?

Any help or suggestions would be greatly appreciated!

Thank you!

(By the way, I'm currently using Windows 11 platform. Both my version of Microsoft Edge and WebDriver are 128.0.2739.42)


Solution

  • Can't reproduce this issue here. I think you can add Thread.Sleep() or Waits before you navigate to the new URL in the new tab.

    As a workaround, you can even directly replace "about:blank" with the target URL. If you prefer to set it separately, you can use:

    Dim url As String = "https://www.google.com/"
    jsExecutor.ExecuteScript($"window.open('{url}', '_blank');")