Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
在新选项卡中打开链接并在选项卡之间切换(Selenium WebDriver和Python)_Python_Selenium Webdriver - Fatal编程技术网

在新选项卡中打开链接并在选项卡之间切换(Selenium WebDriver和Python)

在新选项卡中打开链接并在选项卡之间切换(Selenium WebDriver和Python),python,selenium-webdriver,Python,Selenium Webdriver,我有这样的代码和评论: import selenium.webdriver as webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.common.keys import Keys browser = webdriver.Chrome() browser.get("https://www.google.com?q=python#q=python") first_result = ui.Web

我有这样的代码和评论:

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys


browser = webdriver.Chrome()
browser.get("https://www.google.com?q=python#q=python")
first_result = ui.WebDriverWait(browser, 15).until(lambda browser: 
browser.find_element_by_class_name("rc"))
first_link = first_result.find_element_by_tag_name("a")

# Save the window opener (current window, do not mistake with tab... 
not the same).
main_window = browser.current_window_handle

# Open the link in a new tab by sending keystrokes on the element.
first_link.send_keys(Keys.COMMAND + "t")

# Switch tab to the new tab, which we will assume is the next one on 
the right and put focus.
browser.find_element_by_tag_name("body").send_keys(Keys.COMMAND + 
Keys.NUMPAD2)

# Close current tab.
browser.find_element_by_tag_name("body").send_keys(Keys.COMMAND + "w")

# Put the focus on the current window which will be the window opener.
browser.switch_to.window(main_window)
但是它不起作用(脚本挂起)——第一个链接没有在新选项卡中打开。 还有其他想法吗?谢谢

PS:我在macOS上。

您可以使用以下代码:

driver.execute_script("window.open('http://google.com', 'new_window')")
用于切换:

driver.switch_to_window(driver.window_handles[0])

请参阅此项。

以下代码正在工作:

from selenium import webdriver
from selenium.webdriver.support import ui
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


browser = webdriver.Chrome()
browser.get("https://www.google.com?q=python#q=python")
first_result = ui.WebDriverWait(browser, 15).until(lambda browser: 
browser.find_element_by_class_name("rc"))
first_link = first_result.find_element_by_tag_name("a")

# Save the window opener (current window, do not mistake with tab... not the same).
main_window = browser.current_window_handle

# Open the link in a new tab by sending keystrokes on the element.
ActionChains(browser) \
.key_down(Keys.COMMAND) \
.click(first_link) \
.key_up(Keys.COMMAND) \
.perform()

browser.switch_to.window(browser.window_handles[1])

time.sleep(5)

# Close current tab.
browser.close()

time.sleep(5)

# Put the focus on the current window which will be the window opener.
browser.switch_to.window(main_window)

# Close the instance of the browser.
browser.quit()

谢谢你的帮助

谢谢你的帮助!是的,这不适用于我webdriver.support不适用于我,我有最新的npm@PetertheRussian,???@PetertheRussian,我已经更新了答案(更新了导入包的方式)。过来看。