chrome webdriver 无法打开新标签页
chrome webdriver cannot open new tab
我正在尝试在 Selenium.WebDriver.ChromeDriver" version="2.21.0.0" 中打开一个新选项卡,但它没有打开任何东西,但是如果我将调试跟踪步骤移回行 "body.SendKeys(Keys.Control + 't')" 重新运行第二次,有效 ??
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');
更新:似乎停止了 chrome,它确实打开了标签页。因此,不要使用 Thread.sleep,只需尝试:
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("return window.stop");
body.SendKeys(Keys.Control + 't');
使用以下代码解决您的问题:
Actions act = new Actions(driver);
act.sendKeys(Keys.CONTROL,"t").build().perform();
使用 Chrome 打开新标签页:
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://whosebug.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");
我正在尝试在 Selenium.WebDriver.ChromeDriver" version="2.21.0.0" 中打开一个新选项卡,但它没有打开任何东西,但是如果我将调试跟踪步骤移回行 "body.SendKeys(Keys.Control + 't')" 重新运行第二次,有效 ??
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');
更新:似乎停止了 chrome,它确实打开了标签页。因此,不要使用 Thread.sleep,只需尝试:
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("return window.stop");
body.SendKeys(Keys.Control + 't');
使用以下代码解决您的问题:
Actions act = new Actions(driver);
act.sendKeys(Keys.CONTROL,"t").build().perform();
使用 Chrome 打开新标签页:
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://whosebug.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");