Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在Mac OS X上的selenium webdriver中打开新选项卡_Python_Macos_Selenium_Firefox_Selenium Webdriver - Fatal编程技术网

Python 无法在Mac OS X上的selenium webdriver中打开新选项卡

Python 无法在Mac OS X上的selenium webdriver中打开新选项卡,python,macos,selenium,firefox,selenium-webdriver,Python,Macos,Selenium,Firefox,Selenium Webdriver,我应该能够使用代码在selenium for python中打开一个新选项卡 from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("http://stackoverflow.com/") body = driver.find_element_by_tag_name("body") body.send_keys

我应该能够使用代码在selenium for python中打开一个新选项卡

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + 't')
但不会打开新选项卡,也不会显示错误消息(不会加载)

请注意,我使用的是
Keys.COMMAND+'t'
,因为我是在OSX上运行代码的

我不知道是什么导致了这个问题,因为像这样的帖子表明我的代码应该可以工作

更新以包含答案

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

current_tab = driver.current_window_handle
driver.execute_script('window.open();')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
driver.get("http://github.com")
inputElement = driver.find_element_by_id("user[login]")
inputElement.send_keys('1')

current_tab = driver.current_window_handle
driver.execute_script('window.open();')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
driver.get("http://github.com")
inputElement = driver.find_element_by_id("user[email]")
inputElement.send_keys('2')

尝试以下代码打开新选项卡并切换到该选项卡:

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

current_tab = driver.current_window_handle
driver.execute_script('window.open("http://github.com");')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
inputElement = driver.find_element_by_id("user[login]")
inputElement.send_keys('1')

driver.execute_script('window.open("http://github.com");')
third_tab = [tab for tab in driver.window_handles if tab not in (current_tab, new_tab)][0]
driver.switch_to.window(third_tab)
inputElement = driver.find_element_by_id("user[email]")
inputElement.send_keys('2')
您可以使用
driver.close()
关闭新选项卡,并使用
driver.switch\u to.window(当前选项卡)
切换回初始选项卡

还请注意,您可以将要在新选项卡中打开的页面
URL
作为参数传递给
window.open()
,如:

driver.execute_script('window.open("https://google.com");')

尝试以下代码在MAC中打开新选项卡:-

String clickOnTabLink=Keys.chord(Keys.COMMAND,“t”,Keys.ENTER);
link.sendKeys(单击链接);

尝试
驱动程序。执行脚本('window.open();')
instead@Andersson成功了。。。。我不知道为什么。您知道如何切换到新的“打开”选项卡吗。如果我能理解,我会考虑回答的问题。非常有用。再问一个问题,如果你看我发布的更新,而不是切换到最新的选项卡,那么脚本将返回到第一个选项卡。我不知道为什么会这样。当您搜索电子邮件输入字段时,不需要再次切换并打开Github。我想你误解了我的问题。我正在尝试在两个不同的选项卡中打开同一个网站两次,并在两个不同的字段中添加文本。哦。。。我懂了。尝试
third\u tab=[driver.window中tab的tab\u句柄,如果tab不在(当前的选项卡,新的选项卡)][0]
。请原谅,创建一个新的标签,然后加载网页需要两个单独的命令,否则它无法找到文本字段。