[英]How to open a new chrome tab using selenium webdriver?
Upon researching, I found out that Control + t doesn't work for the Chrome webdriver. 经过研究,我发现Control + t不适用于Chrome网络驱动程序。 The following are some that I've tried and did not work:
以下是我尝试过但无法使用的一些内容:
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.get("www.facebook.com");
and 和
WebElement element = driver.findElement(By.linkText("Gmail"));
Actions actionOpenLinkInNewTab = new Actions(driver);
actionOpenLinkInNewTab.moveToElement(element)
.keyDown(Keys.COMMAND)
.keyDown(Keys.SHIFT)
.click(element)
.keyUp(Keys.COMMAND)
.keyUp(Keys.SHIFT)
.perform();
ArrayList tabs = new ArrayList (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://www.yahoo.com");
driver.close();
driver.switchTo().window(tabs.get(0));
driver.get("http://www.yahoo.com");
driver.close();
and 和
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
}
Were any of you able to step around this? 你们中的任何人都可以解决这个问题吗?
C# C#
Use this code to open new tab & switch between tabs: 使用此代码打开新标签页并在标签页之间切换:
Use Waiter. 使用服务员。
tabs index starts from 0 for 1st tab. 对于第一个选项卡,tabs索引从0开始。
var body = Waiter.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.TagName("body"))).FirstOrDefault();
body.SendKeys(Keys.Control + 't');
var tabs = GlobalDriver.WindowHandles;
GlobalDriver.SwitchTo().Window(tabs[1]);
GlobalDriver.Navigate().GoToUrl("Url");
The code is in C# 代码在C#中
It will be somehow similar in Java , just the syntax will be different. 它在Java中会有些相似,只是语法会有所不同。
Hope this helps you! 希望这对您有所帮助!
You can use Java Robot to send Ctrl+t (or Cmd+t if MAC OS X) and the wait for the new handle to be opened, for example: 您可以使用Java Robot发送Ctrl + t(如果是MAC OS X,则发送Cmd + t)并等待新句柄被打开,例如:
// Open URL in default tab
driver.get("https://wikipedia.org/");
// If Mac OS X, the key combination is CMD+t, otherwise is CONTROL+t
int vkControl = IS_OS_MAC ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
Robot robot = new Robot();
robot.keyPress(vkControl);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(vkControl);
robot.keyRelease(KeyEvent.VK_T);
// Wait up to 5 seconds to the second tab to be opened
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
// Switch to new tab
List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
System.err.println(windowHandles);
driver.switchTo().window(windowHandles.get(1));
// Open other URL in second tab
driver.get("https://google.com/");
Here you have a running example using Chrome as browser. 在这里,您有一个使用Chrome作为浏览器的运行示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.