See comment above. Was getting inconsistent return values from driver.getWindowHandles() which was causing the wrong window to be switched to.