chrome webdriver无法打开新标签

时间:2016-04-06 09:31:35

标签: c# selenium selenium-chromedriver

我正在尝试在Selenium.WebDriver.ChromeDriver“version =”2.21.0.0“中打开一个新标签,但它不会打开任何内容,但是如果我将调试跟踪步骤移回”正文“行。 SendKeys(Keys.Control +'t')“第二次重新运行,它有效吗?

>>> {k:min(v or [-1]) for k,v in mydict.items()}
{70: 1, 40: -1, 10: -1, 80: 2, 50: 1, 20: -1, 90: 1, 60: -1, 30: -1}
>>> {k:max(v or ["empty list"]) for k,v in mydict.items()}
{70: 1, 40: 'empty list', 10: 'empty list', 80: 7, 50: 1, 20: 'empty list', 90: 6, 60: 'empty list', 30: 'empty list'}

更新:它似乎停止了Chrome,它确实打开了标签。所以不要使用Thread.sleep,只需尝试:

  var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
  IWebElement body = wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("body")));                          
                            Thread.Sleep(2000);
                            body.SendKeys(Keys.Control + 't');

2 个答案:

答案 0 :(得分:2)

使用Chrome打开新标签页:

    var driver = new ChromeDriver();

    driver.Navigate().GoToUrl("http://stackoverflow.com");

    // open a new tab and set the context
    driver.ExecuteScript("window.open('_blank', 'tab2');");
    driver.SwitchTo().Window("tab2");

    driver.Navigate().GoToUrl("https://www.google.com");

答案 1 :(得分:0)

针对您的问题使用以下代码:

Actions act = new Actions(driver);
act.sendKeys(Keys.CONTROL,"t").build().perform();