toom toom - 10 months ago 141
Python Question

Open new tab in Firefox using Selenium WebDriver on Mac

I just installed Selenium Web Driver and tried it out. It works great. My use case can be describe as followed:

  1. Start Firefox on a server with pseudo X server (Xvfb)

  2. New Driver.Firefox() object

  3. Open 10 tabs and load a webpage in each tab

  4. Retrieve the html from all loaded pages

The only step that is not working is step 3. I can not find out how to open new tabs. I found this here on SO : How to open a new tab using Selenium WebDriver? However, I tested this locally (i.e. with visible display) on my Mac for debugging purpose and I saw that the Firefox browser (which was opened when creating the driver object) does not open any tabs when doing as described on the SO thread. So I tried this here:

driver = webdriver.Firefox()
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')

As I said, it does not work for me. So, how else is it possible to open tabs? I use Selenium 2.39 (pip install selenium) and Python 2.7.


the key combination to open a new tab on OSX is Command+T, so you should use

body.send_keys(Keys.COMMAND + 't')