简体   繁体   English

C#Selenium firefox打开新选项卡不起作用

[英]c# selenium firefox open new tab is not working

i try this codes and open mozila firefox the get google.com site but never open new tab why? 我尝试使用此代码并在get google.com网站上打开mozila firefox,但从未打开新标签,为什么?

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://google.com");
            IWebElement element = driver.FindElement(By.TagName("Body"));
            System.Threading.Thread.Sleep(6000);
            element.SendKeys(OpenQA.Selenium.Keys.Control + "t");
        }
    }
}

and try this but it has never open new tab! 并尝试此操作,但它从未打开过新标签!

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://google.com");
            var action = new Actions(driver);
            System.Threading.Thread.Sleep(6000);
            action.KeyDown(Keys.Control).SendKeys("t").Perform();
        }
    }
}

You can use javascript executor for this :- 您可以为此使用javascript executor:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open('https://www.google.com','_blank');");



namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {


 IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://google.com");
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
          js.ExecuteScript("window.open();");
        }
    }
}

Refer this link :- How to handle the new window in Selenium WebDriver using Java? 请参阅此链接:- 如何使用Java处理Selenium WebDriver中的新窗口?

Find body and then send keys. 查找body ,然后send密钥。

 static void Main(string[] args) { IWebDriver driver = new FirefoxDriver(); driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t"); driver.SwitchTo().Window(driver.WindowHandles.Last()); driver.Navigate().GoToUrl("https://www.google.com") } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM