Python 在 OS X 中使用 Selenium WebDriver 打开和关闭新选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25951968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open and close new tab with Selenium WebDriver in OS X
提问by user2314737
I'm using the Firefox Webdriver in Python 2.7 on Windows to simulate opening (Ctrl+t) and closing (Ctrl+ w) a new tab.
我在 Windows 上的 Python 2.7 中使用 Firefox Webdriver 来模拟打开 ( Ctrl+ t) 和关闭 ( Ctrl+ w) 新选项卡。
Here's my code:
这是我的代码:
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')
How to achieve the same on a Mac?
Based on this commentone 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?
如何在 Mac 上实现相同的目标?基于此评论,人们应该使用它browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')来打开一个新选项卡,但我没有 Mac 来测试它,那么等效的Ctrl-w?
Thanks!
谢谢!
采纳答案by Bek
There's nothing easier and clearer than just running JavaScript.
没有什么比运行 JavaScript 更简单、更清晰的了。
Open new tab:
driver.execute_script("window.open('');")
打开新标签:
driver.execute_script("window.open('');")
回答by ZijiG
Open new tab:
打开新标签:
browser.execute_script("window.open('"+your url+"', '_blank')")
Switch to new tab:
切换到新标签:
browser.switch_to.window(windows[1])
回答by alien_frog
open a new tab:
打开一个新标签:
browser.get('http://www.google.com')
close a tab:
关闭标签:
browser.close()
switch to a tab:
切换到标签:
browser.swith_to_window(window_name)
回答by 3mrsh
Just to combine the answers above for someone still curious. The below is based on Python 2.7 and a driver in Chrome.
只是为了让仍然好奇的人结合上面的答案。以下基于 Python 2.7 和 Chrome 中的驱动程序。
Open new tab by: driver.execute_script("window.open("+URL+", '__blank__');")where URL is a string such as "http://www.google.com".
Open new tab by:driver.execute_script("window.open("+URL+", '__blank__');")其中 URL 是一个字符串,例如“ http://www.google.com”。
Close tab by:
driver.close()[Note, this also doubles as driver.quit()when you only have 1 tab open].
关闭标签:
driver.close()[注意,driver.quit()当您只打开 1 个标签时,这也会加倍]。
Navigate between tabs by: driver.switch_to_window(driver.window_handles[0])and driver.switch_to_window(driver.window_handles[1]).
通过以下方式在选项卡之间导航:driver.switch_to_window(driver.window_handles[0])和driver.switch_to_window(driver.window_handles[1])。
回答by JayHung
You can choose which window you want to close:
您可以选择要关闭的窗口:
window_name = browser.window_handles[0]
Switch window:
切换窗口:
browser.switch_to.window(window_name=window_name)
Then close it:
然后关闭它:
browser.close()

