Selenium逻辑旅程-如果通过第一个场景,则移动到下一个url-Python

Selenium逻辑旅程-如果通过第一个场景,则移动到下一个url-Python,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在创建一个爬虫程序,它将遍历大量的URL,并遍历许多场景。如果其中一个场景通过了,我想写一些东西,然后再次检查场景中的下一个url,直到通过为止 from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common

我正在创建一个爬虫程序,它将遍历大量的URL,并遍历许多场景。如果其中一个场景通过了,我想写一些东西,然后再次检查场景中的下一个url,直到通过为止

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC

    options = Options()
    options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(chrome_options=options)

    urls = ['https://www.google.com/', 'https://stackoverflow.com/']

    for url in urls:
        driver.get(url)
        image_name = url.split(".")[1] + ".png"
        driver.save_screenshot(image_name)
        performance_data = driver.execute_script('return window.performance.getEntries();')
        for single_data in performance_data:
            if "nav" in single_data["name"]:
                file.write(url + "adserv_1")
                break
#over here the loop should break and look for the new url rather than continuing the below?    

        driver.execute_script("window.scrollTo(0,1000);")
        sleep(2)
        driver.execute_script("window.scrollTo(0,5000);")
        sleep(2)
        driver.execute_script("window.scrollTo(0,10000);")
        sleep(2)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        performance_data = driver.execute_script('return window.performance.getEntries();')
        for single_data in performance_data:
            if "nav" in single_data['name']:
                results.write(url + "3")

如果第一个场景通过,代码不应该转到数组中的下一个url吗?

您的“中断”仅退出“for single data”循环,而不是“for url”循环

这是一个简单的解决方法:在第一次中断之前设置一个布尔变量,然后在内部循环之外检查该变量,如果为true,则再次中断。