Can't open a link as a background tab - Java,

2019-09-17 02:44发布

问题:

So of course in internet explorer (9) I cannot get a link to open in a background tab. I've tried a few things :

Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
driver.findElement(By.name(THE_LINK)).click();
r.keyRelease(KeyEvent.VK_CONTROL);

I've also tried

String link = Keys.chord(Keys.CONTROL, Keys.RETURN);
driver.findElement(By.name(THE_LINK)).sendKeys(link);

It actually opens it in a new window.

When I'm working manually and ctrl+click it works as expected and opens in a background tab. I've tried resetting browser settings and doing a lot of different things but of course, it doesn't work as expected.

Anyone seen this before? Anyone got a different thing for me to try?

回答1:

There is no reason to do this.

If you click the link, just switch back to the original context by using:

driver.switchTo().defaultContent();

to get back to the tab you were on.

If that doesn't work, you might have to do

driver.switchTo().window(driver.getWindowHandles()[0]);  // i'm not at my programming computer so i can't remember the actual syntax ;)