driver.quit()与driver.close()的不同:
driver.quit(): Quit this driver, closing every associated windows;
driver.close(): Close the current window, quiting the browser if it is the last window currently open.
翻译成中文:
driver.close() -- 关闭当前窗口,如果是当前打开的最后一个窗口,则退出浏览器
driver.quit() -- 退出驱动,关闭所有相关的窗口
代码附录:
from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get('http://sahitest.com/demo/index.htm') print(driver.current_window_handle) # 查看当前window handle driver.find_element_by_link_text('Window Open Test').click() # 打开新window1 driver.find_element_by_link_text('Window Open Test With Title').click() # 打开新window2 print(driver.window_handles) # 查看所有window handles driver.close() print(driver.window_handles) # 查看现在的所有window handles,可看到只是关闭了最开始的一个window,其他两个window还在 driver.quit() # 看到所有window都被关闭