如何在python selenium中选择最近的浏览窗口

如何在python selenium中选择最近的浏览窗口,python,selenium,Python,Selenium,当我运行以下代码时,它会打开两个浏览窗口;第二个是当代码点击“提交者”按钮时自动打开的 from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = webdriver.Firefox() url = 'http://example.com' browser.get(url) browser.find_element_by_id('submitOrder').click(

当我运行以下代码时,它会打开两个浏览窗口;第二个是当代码点击“提交者”按钮时自动打开的

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

browser = webdriver.Firefox()
url = 'http://example.com'
browser.get(url)    
browser.find_element_by_id('submitOrder').click()
现在我必须在第二个窗口(即最近的浏览窗口)点击“结帐”按钮。但是下面的代码给出的错误是没有这样的元素

browser.find_element_by_id('checkout').click()

如何选择最近的浏览窗口(即,不是上一个)?

要单击“签出”按钮,您必须首先切换到该按钮所属的窗口,然后对该按钮执行操作

driver.switch_to_window(handle) 

windowName详细信息

对驱动程序的所有调用现在都将被解释为指向特定窗口。但是你怎么知道窗户的名字?查看打开它的javascript或链接:

有关更多详细信息,请参阅

谢谢您的努力。顺便说一句,我怎么知道windowName?你可以在上面提到的链接中找到它。通过输入详细信息更新答案。如果
windowName
不适用于您的情况,您可以使用
handle
。并接受答案,如果它对你有帮助的话:)
driver.switch_to_window("windowName")
All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:

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