简体   繁体   English

Ubuntu上的Python Selenium Firefox:新选项卡不起作用

[英]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. 我正在进行远程Web爬网和抓取,希望不要为一页上的每个链接重新加载新的浏览器窗口。

The problem is that new tabs are not opening up with my Firefox web driver. 问题是我的Firefox Web驱动程序没有打开新标签页。

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 Ubuntu 14.04.2
  • Python 2.7.6 Python 2.7.6
  • Selenium 2.47.1 硒2.47.1
  • PyVirtualDisplay 0.1.3 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. 根据Selenium开发人员的回复 ,截至2015年8月,Firefox中不支持新选项卡。他建议研究Marionette,但目前,其依赖关系会带来更多麻烦,至少在我的用例中。 His solution is to just use new windows ( driver.execute_script("window.open()") ), instead of new tabs. 他的解决方案是仅使用新窗口( driver.execute_script("window.open()") ),而不是新选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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