问题
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