简体   繁体   English

如何使用Selenium Webdriver打开新的Chrome标签页?

[英]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.

相关问题 如何使用 Selenium WebDriver 在新标签页(chrome)中打开链接? - How to open a link in new tab (chrome) using Selenium WebDriver? 如何使用硒在 chrome 中打开一个新标签 - How to open a new tab in chrome using selenium 如何单击新标签中的打开链接以使用 selenium webdriver 在主标签中显示的新标签中打开链接 - How to click on open link in new tab to open the link in new tab that appears in main tab using selenium webdriver 如何通过将Selenium WebDriver与Java一起在同一浏览器中打开新标签页? - How to open a new tab in the same browser by using Selenium WebDriver with Java? 如何在 Java 中使用 Selenium WebDriver 打开一个新选项卡? - How to open a new tab using Selenium WebDriver in Java? Selenium chrome webDriver - 关注最新打开的新标签java - Selenium chrome webDriver - get focus on the latest open new tab java 如何使用 Selenium 在 Chrome 浏览器上打开新标签页 - How to open new tab on Chrome browser using Selenium 如何使用Selenium在Chrome中打开新标签页并在Java中切换到新标签页 - How to open a new tab in Chrome and switch to that new tab in Java using Selenium 使用 Selenium WebDriver 的新标签 - New Tab using Selenium WebDriver 如何使用Selenium WebDriver在新选项卡中打开网页 - How to open a web page in a new tab with Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM