selenium遍历循环网页报错

我在爬取淘宝的时候,使用selenium打开了一个网页,提取到里面所有的url,模拟点击操作里面的url,当点进去第一个链接以后我把信息提取完就关闭当前这个网页,在第二次循环遍历的时候就报错了。求大神指点这个是为什么?能不能帮我实现一下这个功能,点击完一个url提取信息以后就关闭,继续点击下一个url。直到退出循环

from selenium import webdriver
from time import sleep
import json
from random import randint

class Taobao():
    def __init__(self):
        self.options = webdriver.ChromeOptions()
        self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
        self.driver = webdriver.Chrome(options=self.options)

    def get_cookies(self, url):
        #获取登录cookie
        self.driver.get(url)
        sleep(20)
        with open('tb_cookie.txt', 'w') as f:
            f.write(json.dumps(self.driver.get_cookies()))
        # self.driver.quit()

    def have_cookies(self, url):
        #提取需要登录的cookie
        self.driver.get(url)
        with open('tb_cookie.txt', 'r')as f:
            cookie_list = json.loads(f.read())
            for cookie in cookie_list:
                self.driver.add_cookie({
                    'domain': '.taobao.com',
                    'name': cookie['name'],
                    'value': cookie['value'],
                    'path': '/',
                    'expires': None
                })
        sleep(randint(7,15))
        # print(self.driver.page_source)

    def have_html(self,url):
        self.driver.get(url)
        m = self.driver.find_elements_by_xpath('//div[@id="J_u_root"]/div/div/ul/li/a')
        for i in m:
            sleep(randint(7, 15))
            i.click()
            #获取当前网页后信息后关闭网页
            windows = self.driver.window_handles
            self.driver.switch_to.window(windows[1])
            sleep(randint(7, 15))
            # self.driver.switch_to.window(windows[0])
            self.driver.close()

if __name__ == '__main__':
    url = 'https://login.taobao.com/member/login.jhtml?'
    urls = 'https://ai.taobao.com/search/index.htm?spm=a231o.13503973.search.1&key=手机'

    t = Taobao()
    # t.get_cookies(url)
    t.have_cookies(url)
    t.have_html(urls)


以下是报错信息:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=72.0.3626.121)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.19042 x86_64)

我这边直接运行have_html是没有报错的,have_html的url好似不用登录多可以访问的。

            windows = self.driver.window_handles
            self.driver.switch_to.window(windows[-1])
            # sleep(randint(7, 15))
            # self.driver.switch_to.window(windows[0])
            self.driver.close()
            windows = self.driver.window_handles
            self.driver.switch_to.window(windows[-1])

你可以尝试每次在关闭子页面后再将浏览器再转换窗口句柄,像这样看可否:
for i in range(1,4):
driver.find_element_by_xpath('//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[{}]/div/card-content/mee-rewards-daily-set-item-content/div/div[3]/a'.format(i)).click()
driver.switch_to.window(driver.window_handles[-1])
time.sleep(30)
driver.close()
driver.switch_to.window(driver.window_handles[-1])#或者driver.switch_to.window(driver.window_handles[0])

你好,看了你的问题是访问url的时候出现的问题,可以参考(https://blog.csdn.net/qq_42994177/article/details/108670601)这篇文章中访问url的方法哦