Selenium不会在新标签页中打开新的网址(Python和Chrome) [英] Selenium won't open a new URL in a new tab (Python & Chrome)

查看:3047
本文介绍了Selenium不会在新标签页中打开新的网址(Python和Chrome)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Selenium WebDriver&



我不确定发生了什么问题:

  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的运行:



作为一种解决方法,您可以做的是在新标签页中打开链接,然后切换到新的窗口w 使用 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在新窗口中的链接
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])$ ​​b $ b driver.get(https://stackoverflow.com)

现在最后一个 driver.get()会是在新打开的标签中执行。


I want to open quite a few URLs in different tabs using Selenium WebDriver & Python.

I am not sure what is going wrong:

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)

I looked up tutorials and it seems to me as though this code should do what I want. What actually happens is the browser opens, url1 opens as it should, a new tab opens as it should but url2 then loads in the original tab instead of the new one (even though the new tab appears to be the active one).

(I am using Chrome because when using Firefox I can't get it to load any URLs at all. Firefox opens but does not get the url requested. I have tried to find a solution to this but to no avail.)

Is there anything I can change in my code to get the new URL to open in the new tab?

Thanks for your help!

解决方案

There is a bug in ChromeDriver that prevents ctrl/command+T from working:

What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:

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")

Now the last driver.get() would be performed in a newly opened tab.

这篇关于Selenium不会在新标签页中打开新的网址(Python和Chrome)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆