您可以通过组合键
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()


