如何使用Selenium C#打開新標籤頁?


1

這是我編寫的代碼,但是沒有用。任何建議都會有所幫助

    IWebDriver driver = new ChromeDriver(@"C:\Selenium Automation\");
    driver.Navigate().GoToUrl("https://thealivecard.com/");

   Actions action = new Actions(driver);

    action
        .KeyDown(Keys.Control)
        .SendKeys("t")
        .KeyUp(Keys.Control)
        .Build().Perform(); 

    Console.WriteLine("new tab");
0

This is what I use to open and switch to a new tab in C#:

((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());

The sending of keys seems to not always work. Depends on the page your are on, the focus state of the browser. While the JavaScript window.open always seems to work.