我在 VB.NET 中使用 Selenium WebDriver 项目时遇到了问题。我尝试在 Edge 中打开一个新选项卡并导航到特定 URL,但该选项卡仍然为空白。以下是相关代码片段:
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
我已确保 WebDriver 和 Edge 浏览器都是最新的。我是否可能遗漏了其他配置或设置?
任何帮助或建议都将不胜感激!
谢谢你!
(顺便说一下,我目前使用的是 Windows 11 平台。我的 Microsoft Edge 和 WebDriver 版本都是 128.0.2739.42)
无法在此处重现此问题。我认为您可以在新选项卡中导航到新 URL 之前添加
Thread.Sleep()
或等待。作为一种解决方法,您甚至可以直接用目标 URL 替换“about:blank”。如果您希望单独设置,则可以使用: