简体   繁体   中英

Python Selenium Firefox on Ubuntu: New Tabs Not Working

I'm doing remote web crawling and scraping, and hoping not to reload a new browser window for every link on one page.

The problem is that new tabs are not opening up with my Firefox web driver.

Here's what I've tried:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from pyvirtualdisplay import Display

# launch our headless display 
display = Display(visible=0, size=(800, 600))
display.start()

# launch our web driver and get a page
browser = webdriver.Firefox()
browser.get("http://www.google.com/")

# try to open a new tab
ActionChains(browser).key_down(Keys.CONTROL).send_keys("t").key_up(Keys.CONTROL).perform()

# this should print 2, but it prints 1, because new tab not opened
print len(browser.window_handles)

# clean up everything
browser.quit()
display.stop()

Specifications:

  • Ubuntu 14.04.2
  • Python 2.7.6
  • Selenium 2.47.1
  • PyVirtualDisplay 0.1.3

Based on this response from a Selenium developer , new tabs in Firefox are not supported as of August 2015. He suggested exploring Marionette but currently its dependencies cause more trouble than it's worth, at least for my use case. His solution is to just use new windows ( driver.execute_script("window.open()") ), instead of new tabs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM