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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37088589/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:49:11  来源:igfitidea点击:

Selenium won't open a new URL in a new tab (Python & Chrome)

pythongoogle-chromeselenium

提问by SamH123

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

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

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 buturl2 then loads in the original tab instead of the new one (even though the new tab appears to be the active one).

我查找了教程,在我看来,这段代码应该可以满足我的要求。实际发生的是浏览器打开,url1 按原样打开,一个新选项卡按原样打开,url2 然后加载原始选项卡而不是新选项卡(即使新选项卡似乎是活动选项卡)。

(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.)

(我使用 Chrome 是因为在使用 Firefox 时我根本无法让它加载任何 URL。Firefox 打开但没有获取请求的 URL。我试图找到解决方案,但无济于事。)

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

有什么我可以在我的代码中更改以获得在新选项卡中打开新 URL 的内容吗?

Thanks for your help!

谢谢你的帮助!

回答by Mihai

Here is a simple way, platform independent:

这是一种简单的方法,独立于平台:

Code:

代码:

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

Switching back to the original tab:

切换回原始选项卡:

Code:

代码:

driver.switch_to_window(driver.window_handles[0])

Checking the current title to be sure you are on the right page:

检查当前标题以确保您在正确的页面上:

Code:

代码:

driver.title

For everything else, have fun!

对于其他一切,玩得开心!

回答by alecxe

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

ChromeDriver 中存在一个错误,导致 ctrl/command+T 无法正常工作:

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

你可以做什么,作为一种解决方法,是在新标签中打开一个链接,然后切换到一个新的窗口使用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")

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

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

回答by Gemtastic

An alternative way to open a new window is to use JavaScript and the window handler to switch between them.

打开新窗口的另一种方法是使用 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()

If you look at your browser while you're executing it will look like the new window has focus, but to the webdriver, it doesn't. Don't be fooled by the visual. Also remember to select a new window handler when you close a tab as it will set the driver.current_window_handleto

如果您在执行时查看浏览器,它看起来就像新窗口有焦点,但对于 webdriver 来说,它没有。不要被视觉所迷惑。还要记住在关闭选项卡时选择一个新的窗口处理程序,因为它将设置driver.current_window_handle

selenium.common.exceptions.NoSuchWindowException: 
    Message: no such window: target window already closed from unknown error: web view not found
  (Session info: chrome=<Your version of chrome>)
  (Driver info: chromedriver=<Your chrome driver version> (<string of numbers>),platform=<Your OS>)

on .close()and it will throw that error if you try to do stuff with the driver at that stage.

.close(),如果你试图做的东西与在该阶段,驱动程序将抛出这个错误。

回答by Nihal

you need to maximize your chrome for this

为此,您需要最大化您的 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()

here key_down(Keys.CONTROL)will hold down ctrl key, to get focus on page i am clicking body of the page, then click k

这里key_down(Keys.CONTROL)将按住 ctrl 键,以获得焦点在页面我点击页面的正文,然后点击k