How to close a tab and open a new tab using Seleni

2019-07-28 00:51发布

问题:

I'm working on selenium RC java. In my test case, I have to close a tab and then open a new tab in the same test case. Can anyone please tell me how to do it?

回答1:

Have you tried using Selenium IDE to record these scenarios? That's what I would try first.

Alternatively, try firing a Ctrl+T event to open a new tab:

selenium.keyDownNative("17");
selenium.keyPressNative("84");
selenium.keyUpNative("17"); 

and Ctrl+W to close the tab:

selenium.keyDownNative("17");
selenium.keyPressNative("87");
selenium.keyUpNative("17"); 


回答2:

To open new tab:

Robot r = new Robot(); 
r.keyPress(KeyEvent.VK_CONTROL); 
r.keyPress(KeyEvent.VK_T); 
r.keyRelease(KeyEvent.VK_CONTROL); 
r.keyRelease(KeyEvent.VK_T);

And close the tab opened:

r.keyPress(KeyEvent.VK_CONTROL); 
r.keyPress(KeyEvent.VK_W); 
r.keyRelease(KeyEvent.VK_CONTROL); 
r.keyRelease(KeyEvent.VK_W);