如何在 Selenium for Python 中切换到新窗口?

我正在使用 Python 编写 硒自动化项目

我正面临一个问题,即处理多个浏览器窗口。

场景如下。当我单击主页上的链接时,将打开一个新窗口。在新打开的窗口中,我不能执行任何操作,因为焦点仍然在主页网页驱动程序上。

有人能告诉我如何将焦点从背景窗口转移到新打开的窗口吗?

一个可能的解决方案是 driver.switch_to.window(),但它需要窗口的名称。如何找到窗户的名字?如果这是一种错误的方法,有人能给出一些代码示例来执行这个操作吗?

183293 次浏览

window_handles should give you the references to all open windows.

this is what the documentation has to say about switching windows.

You can do it by using window_handles and switch_to.window method.

Before clicking the link first store the window handle as

window_before = driver.window_handles[0]

after clicking the link store the window handle of newly opened window as

window_after = driver.window_handles[1]

then execute the switch to window method to move to newly opened window

driver.switch_to.window(window_after)

and similarly you can switch between old and new window. Following is the code example

import unittest
from selenium import webdriver


class GoogleOrgSearch(unittest.TestCase):


def setUp(self):
self.driver = webdriver.Firefox()


def test_google_search_page(self):
driver = self.driver
driver.get("http://www.cdot.in")
window_before = driver.window_handles[0]
print window_before
driver.find_element_by_xpath("//a[@href='http://www.cdot.in/home.htm']").click()
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
print window_after
driver.find_element_by_link_text("ATM").click()
driver.switch_to.window(window_before)


def tearDown(self):
self.driver.close()


if __name__ == "__main__":
unittest.main()

We can handle the different windows by moving between named windows using the “switchTo” method:

driver.switch_to.window("windowName")


<a href="somewhere.html" target="windowName">Click here to open a new window</a>

Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

for handle in driver.window_handles:
driver.switch_to.window(handle)

On top of the answers already given, to open a new tab the javascript command window.open() can be used.

For example:

# Opens a new tab
self.driver.execute_script("window.open()")


# Switch to the newly opened tab
self.driver.switch_to.window(self.driver.window_handles[1])


# Navigate to new URL in new tab
self.driver.get("https://google.com")
# Run other commands in the new tab here

You're then able to close the original tab as follows

# Switch to original tab
self.driver.switch_to.window(self.driver.window_handles[0])


# Close original tab
self.driver.close()


# Switch back to newly opened tab, which is now in position 0
self.driver.switch_to.window(self.driver.window_handles[0])

Or close the newly opened tab

# Close current tab
self.driver.close()


# Switch back to original tab
self.driver.switch_to.window(self.driver.window_handles[0])

Hope this helps.

for eg. you may take

driver.get('https://www.naukri.com/')

since, it is a current window ,we can name it

main_page = driver.current_window_handle

if there are atleast 1 window popup except the current window,you may try this method and put if condition in break statement by hit n trial for the index

for handle in driver.window_handles:
if handle != main_page:
print(handle)
login_page = handle
break


driver.switch_to.window(login_page)

Now ,whatever the credentials you have to apply,provide after it is loggen in. Window will disappear, but you have to come to main page window and you are done

driver.switch_to.window(main_page)
sleep(10)