Sending keys (Ctrl+W) to Firefox to close tabs

1,772 views
Skip to first unread message

Maddog

unread,
Dec 1, 2011, 12:28:25 PM12/1/11
to webdriver
Hi,

Is there a way to use the Keyboard to close tabs?
My problem is this - if I allow popups - whenever I try to close them,
firefox crashes and this appears in the event log:

Faulting application name: Firefox.exe, version: 7.0.1.4288, time
stamp: 0x4e83b93a
Faulting module name: xul.dll, version: 7.0.1.4288, time stamp:
0x4e83b840
Exception code: 0xc0000005
Fault offset: 0x000b8acc

So I switched to using tabs - I force firefox to open tabs instead of
popups - that prevents the crashes, but it is impossible to close the
tabs with Browser.Close(); like it was with popups.

But since I'm not recreating the firefox instance after each run - it
fills up with useless tabs.

I tried doing this after each run, but it doesn't work
var keyb = Browser.Keyboard;
for (var i = 0; i < 5; i++ )
{
try
{
Browser.SwitchTo().ActiveElement();
keyb.PressKey(Keys.Control);
keyb.SendKeys("w");
keyb.ReleaseKey(Keys.Control);
}
catch (Exception){}
}

Is there any way to close tabs or closing popups without crashing?
Closing popups used to work when I just started with webdriver (maybe
9 months ago), but it broke at some point and never worked since.

Any tips? Thank you

Maddog

unread,
Dec 1, 2011, 12:36:56 PM12/1/11
to webdriver
I just noticed the other thread talking about Ctrl+F5.
I tried this way as well and it also doesn't work:

new
Actions(Browser).KeyDown(Keys.Control).SendKeys("w").KeyUp(Keys.Control).Perform();

Luke Inman-Semerau

unread,
Dec 1, 2011, 12:42:16 PM12/1/11
to webd...@googlegroups.com
I think you've gone down a dark path ;)

First Browser.Close() should work on a window that's been popped up... if it doesn't, please try to submit a new issue with a reproducible test case. If that's not possible for you and you probably want a work-around right now...

I would say in that new window that is open when you want to close it, use the JavaScriptExecutor to call "window.close();"  If that doesn't work, something more sinister is going on.

-Luke

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Maddog

unread,
Dec 1, 2011, 1:39:16 PM12/1/11
to webdriver
Dark path... you don't say, I felt it all along :) I even thought of
trying something like autohotkey to send the key combination.

I'd really love to see this solved from the webdriver side - I'll try
to make a simple page that exhibits this behavior.

Maddog

unread,
Dec 1, 2011, 1:57:54 PM12/1/11
to webdriver
Well, it was easier than I thought... The following crashes as soon as
it tries to run the DefaultContent (.Net / FF7)

var profile = new FirefoxProfile();
//Allow popups to be opened during page-load
profile.SetPreference("dom.disable_open_during_load", false);

var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://jsfiddle.net/VftEX/1/");

Thread.Sleep(1000);

var windows = driver.WindowHandles.ToList();
windows.RemoveAt(0);

foreach (var win in windows)
{
driver.SwitchTo().Window(win);
driver.Close();
}
Thread.Sleep(100);
driver.SwitchTo().DefaultContent();

Maddog

unread,
Dec 3, 2011, 10:39:29 AM12/3/11
to webdriver
Does anyone see the same firefox crash after the popup closes?

Danny Guerrier

unread,
Dec 3, 2011, 10:50:50 AM12/3/11
to webd...@googlegroups.com
How trying the get default content after ensuring the popup is closed?
Instead of the timeout, wait until the window is one less the previous
before doing the get default content. I suspect a race condition.

Maddog

unread,
Dec 3, 2011, 11:25:44 AM12/3/11
to webdriver
It's just an example, on real pages, where the popup is created and
fully loaded I get the same result.
The browser itself crashes - doesn't matter what comes after
Browser.Close(); I used to have navigation there instead of
DefaultContent().

On Dec 3, 5:50 pm, Danny Guerrier <dguerr...@gmail.com> wrote:
> How trying the get default content after ensuring the popup is closed?
> Instead of the timeout, wait until the window is one less the previous
> before doing the get default content. I suspect a race condition.
>

Maddog

unread,
Dec 3, 2011, 11:39:52 AM12/3/11
to webdriver
FWIW, I tried putting a WebDriverWait that waits after each
Browser.Close(); until the number of window handles is smaller - it
gives the correct window number and then still crashes on next
command.

Maddog

unread,
Dec 6, 2011, 6:31:34 AM12/6/11
to webdriver
Bump? :(
Reply all
Reply to author
Forward
0 new messages