你可以通过组合键
COMMAND+ T或
COMMAND+ W(OSX)实现标签的打开/关闭。在其他操作系统上,你可以使用
CONTROL+ T/ CONTROL+ W。
在硒中,你可以模仿这种行为。你将需要创建一个
WebDriver和所需的选项卡。
这是代码。
from selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Firefox()driver.get("http://www.google.com/")#open tabdriver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') # You can use (Keys.ConTROL + 't') on other OSs# Load a page driver.get('http://stackoverflow.com/')# Make the tests...# close the tab# (Keys.ConTROL + 'w') on other OSs.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') driver.close()


