在OS X中使用Selenium WebDriver打开和关闭新选项卡 [英] Open and close new tab with Selenium WebDriver in OS X
问题描述
我正在Windows的Python 2.7中使用Firefox Webdriver模拟打开( Ctrl + t )和关闭( Ctrl + w )的新标签.
I'm using the Firefox Webdriver in Python 2.7 on Windows to simulate opening (Ctrl+t) and closing (Ctrl + w) a new tab.
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('https://www.google.com')
main_window = browser.current_window_handle
# open new tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
browser.get('https://www.yahoo.com')
# close tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
如何在Mac上实现相同的目标?
根据此评论,人们应该使用browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
打开一个新选项卡,但我没有Mac进行测试,Ctrl-w
的等效选项如何?
How to achieve the same on a Mac?
Based on this comment one should use browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
to open a new tab but I don't have a Mac to test it and what about the equivalent of Ctrl-w
?
谢谢!
推荐答案
没有什么比仅运行JavaScript更容易和更清楚了.
There's nothing easier and clearer than just running JavaScript.
打开新标签页:
driver.execute_script("window.open('');")
这篇关于在OS X中使用Selenium WebDriver打开和关闭新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!