【问题标题】:c# selenium firefox open new tab is not workingc# selenium firefox 打开新标签不起作用
【发布时间】:2018-05-22 06:51:40
【问题描述】:

我尝试使用此代码并打开 mozila firefox 获取 google.com 网站,但从未打开新标签 为什么?

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");
        }
    }
}

试试这个,但它从来没有打开新标签!

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();
        }
    }
}

【问题讨论】:

    标签: c# selenium firefox


    【解决方案1】:

    您可以为此使用 javascript 执行器:-

    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();");
            }
        }
    }
    

    参考这个链接:- How to handle the new window in Selenium WebDriver using Java?

    【讨论】:

    • 请接受我的回答,以便其他人可以从中获得帮助
    • 现在我想转到第一个选项卡 firefox 我该怎么办?
    • 使用 drive.close() 它将关闭您当前的标签
    • 你可以在那里使用 windowhandels 概念
    【解决方案2】:

    找到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")
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 2023-03-19
      • 2017-01-31
      相关资源
      最近更新 更多