1

I have a problem. I want to click for button and open link in new tab. This my code:

Actions actions = new Actions(this.driver);
actions.keyDown(Keys.CONTROL).click(myButton).keyUp(Keys.CONTROL).perform();

But link is opened on current tab, not new tab. This is bug of that?

P.S. on chrome and firefox this code work very well

Andrew Bystrov
  • 181
  • 1
  • 14
  • 1
    Possible duplicate of [How to open a new tab using Selenium WebDriver with Java?](http://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver-with-java) – Y.Kaan Yılmaz Mar 22 '16 at 13:52
  • It's not a duplicate, because i need open new tab when i click to button and this problem reproduce only on internet explorer driver – Andrew Bystrov Mar 22 '16 at 14:09

2 Answers2

1

Your code is fine but the IEDriver is not quite as good as the ones for FireFox and Chrome. I had similar issues, as you can read here. I ended up solving my issues with the java.awt.Robot. In your case that would mean you'd need an IE-specific method for opening in new tab.

Robot r = null;
try {
    r = new Robot();
} catch (AWTException e) {
    e.printStackTrace();
}
...
r.setAutoDelay(30);
r.keyPress(KeyEvent.VK_CONTROL);

//then click the button

r.keyRelease(KeyEvent.VK_CONTROL);

It's not elegant but it works.

Community
  • 1
  • 1
David Baak
  • 944
  • 1
  • 14
  • 22
1

One solution would be to open a new window before clicking the link :

WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
// open a new window
((JavascriptExecutor)driver).executeScript("window.open(window.location.href, '_blank');");
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);
// click the link
driver.findElement(By.linkText("Stack Overflow")).click();

Another solution would be to set the target attribute on the targeted element:

WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
WebElement element = driver.findElement(By.linkText("Stack Overflow"));
// set the target _blank on the link
((JavascriptExecutor)driver).executeScript("arguments[0].target='_blank';", element);
// click the link
element.click();
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Thanks for answer. The first solution is works, and i will use it. But i can't understand, why internet explorer driver not implement clicks with modifiers – Andrew Bystrov Mar 22 '16 at 14:13