区别:
driver.quit():关闭整个浏览器
driver.close():关闭当前所在的句柄窗口
示例代码:
from selenium import webdriver
from time import sleep
class Demo1():
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def test_quit_close(self):
self.driver.get('http://www.baidu.com')
sleep(3)
#获取当前窗口句柄
current_handle = self.driver.current_window_handle
print("current_handle:",current_handle)
#跳转到新窗口
el = self.driver.find_element_by_link_text('省批次线').click()
sleep(2)
#获取打开的所有窗口
all_handle = self.driver.window_handles
print('all_handle',all_handle)
for handle in all_handle:
if handle != current_handle:
self.driver.switch_to.window(handle)
sleep(3)
self.driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div/div/div/div/div[2]/div[4]/div/ul/li[9]/a').click()
sleep(2)
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
Demo1().test_quit_close()