Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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不会在新选项卡(Python和Chrome)中打开新URL_Python_Google Chrome_Selenium - Fatal编程技术网

Selenium不会在新选项卡(Python和Chrome)中打开新URL

Selenium不会在新选项卡(Python和Chrome)中打开新URL,python,google-chrome,selenium,Python,Google Chrome,Selenium,我想使用SeleniumWebDriver和Python在不同的选项卡中打开相当多的URL 我不确定出了什么问题: driver = webdriver.Chrome() driver.get(url1) time.sleep(5) driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t') url2 = 'https://www.google.com' driver.get(item2) 我查阅了教程,在我看来,这段

我想使用SeleniumWebDriver和Python在不同的选项卡中打开相当多的URL

我不确定出了什么问题:

driver = webdriver.Chrome()
driver.get(url1)
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
url2 = 'https://www.google.com'
driver.get(item2)
我查阅了教程,在我看来,这段代码应该满足我的要求。实际发生的情况是浏览器打开,url1按其应该的方式打开,新选项卡按其应该的方式打开,但url2随后加载到原始选项卡而不是新选项卡中,即使新选项卡看起来是活动的

我使用Chrome是因为使用Firefox时,我根本无法让它加载任何URL。Firefox打开,但未获取请求的url。我曾试图找到一个解决办法,但无济于事

在我的代码中有什么我可以更改的,以便在新选项卡中打开新的URL吗


谢谢你的帮助

ChromeDriver中存在一个阻止ctrl/command+T工作的错误:

作为一种解决方法,您可以在新选项卡中打开一个链接,然后使用switch_to.window切换到一个新窗口。工作样本:

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

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# open a link in a new window
actions = ActionChains(driver)
about = driver.find_element_by_link_text('About')
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()

driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")

现在,最后一个driver.get将在新打开的选项卡中执行。

这里有一个简单的方法,与平台无关:

代码:

driver.execute_script("window.open('http://google.com', 'new_window')")
driver.switch_to_window(driver.window_handles[0])
driver.title
切换回原始选项卡:

代码:

driver.execute_script("window.open('http://google.com', 'new_window')")
driver.switch_to_window(driver.window_handles[0])
driver.title
检查当前标题以确保您位于正确的页面上:

代码:

driver.execute_script("window.open('http://google.com', 'new_window')")
driver.switch_to_window(driver.window_handles[0])
driver.title

对于其他的一切,祝你快乐

打开新窗口的另一种方法是使用JavaScript和窗口处理程序在它们之间切换

driver = webdriver.Chrome()

# Open a new window
# This does not change focus to the new window for the driver.
driver.execute_script("window.open('');")

# Switch to the new window
driver.switch_to.window(driver.window_handles[1])
driver.get("http://stackoverflow.com")

# close the active tab
driver.close()

# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])
driver.get("http://google.se")

# Close the only tab, will also close the browser.
driver.close()
如果您在执行浏览器时查看它,则新窗口看起来有焦点,但对于webdriver来说,它没有。不要被视觉所愚弄。还记得在关闭选项卡时选择一个新的窗口处理程序,因为它会将driver.current\u window\u句柄设置为


打开。关闭,如果您试图在该阶段对驱动程序执行操作,它将抛出该错误。

为此,您需要最大化您的chrome浏览器

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

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/")

e = driver.find_element_by_tag_name("body")
ActionChains(driver).key_down(Keys.CONTROL).click(e).send_keys("k").key_up(Keys.CONTROL).perform()

这里key_downKeys.CONTROL将按住ctrl键,以获得对页面的关注我正在单击页面的主体,然后单击k

谢谢,但该错误似乎是关于ctrl-t根本不打开新选项卡。我可以很好地打开一个新选项卡,但不能在该选项卡中加载url。我试过你的代码,但可能没有正确理解。“按元素查找元素”链接线上出现错误,无法找到元素。我正在加载一个html页面和一个页面源,我不确定这是否有区别。@SamH123好的,在你的情况下,只是你需要执行驱动程序。切换到.windowdriver。打开新选项卡后,window\u处理[-1]。我使用的是Chrome webdriver,它实际上在一个新选项卡中打开,非常感谢我在geckodriver 0.24.0中使用Firefox v67.0.4,它会打开一个新窗口,但不会打开一个新选项卡如果我想打开两个新选项卡,代码不起作用,它只会打开一个新选项卡。@baermathias,代码是否打开新选项卡或新窗口取决于用户设置。@SafwanSamsudeen应如何配置用户设置?