chrome webdriver cannot open new tab

给你一囗甜甜゛ 提交于 2019-12-08 12:50:23

问题


I'm trying to open up a new tab in Selenium.WebDriver.ChromeDriver" version="2.21.0.0" but it doesn't open anything, however if I move the debug tracking step back to the line "body.SendKeys(Keys.Control + 't')" to rerun the second time, it works ??

  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');

Update: It seems to put a stop on chrome, it does open the tab properly. So instead of using Thread.sleep, just try:

 IJavaScriptExecutor js = driver as IJavaScriptExecutor;
 js.ExecuteScript("return window.stop");
 body.SendKeys(Keys.Control + 't');

回答1:


To open a new tab with 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");



回答2:


Use the following code for your problem :

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


来源:https://stackoverflow.com/questions/36447041/chrome-webdriver-cannot-open-new-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!