首页 > 解决方案 > 为什么for循环不移动到列表中的第二项

问题描述

我有一个嵌套循环,它应该在列表中的第一项上正常工作,但随后完成不确定为什么它不移动第二项?

serials =['BHT350018400', 'BHTM0016380']
url_latency = 'https://my_company/ui/index.html#'
file_system_start_string = '/filesystems/unif/'
file_system_end_string = '__FILESYSTEM__fs_'
section = 'performance'
count = 1
file_system_count = (str(count))

for serial in serials:
    for file in serial:
        while count < 5:
            full_url = url_latency + file_system_start_string + serial + file_system_end_string + file_system_count + '/' + section
            driver.get(full_url)
            driver.maximize_window()
            WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="shell-plugin-area"]/div/div/div/div[2]/ciq-tab-panel/div/div/div/div[3]/div/div/div/div/div/div/div[2]/ciq-performance-details/div/div[1]')))
            screenShot_path =  "\screengrab" + file_system_count + ".png"
            screenshot = driver.save_screenshot(file_path +  screenShot_path)
            time.sleep(1)
            count += 1
            file_system_count = (str(count))
            print(full_url)

标签: pythonnested-loops

解决方案


您需要像这样重置“count”变量,例如:

serials =['BHT350018400', 'BHTM0016380']
url_latency = 'https://my_company/ui/index.html#'
file_system_start_string = '/filesystems/unif/'
file_system_end_string = '__FILESYSTEM__fs_'
section = 'performance'
count = 1
file_system_count = (str(count))

for serial in serials:
    for file in serial:
        count=1 # <------------ here
        while count < 5:
            full_url = url_latency + file_system_start_string + serial + file_system_end_string + file_system_count + '/' + section
            driver.get(full_url)
            driver.maximize_window()
            WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="shell-plugin-area"]/div/div/div/div[2]/ciq-tab-panel/div/div/div/div[3]/div/div/div/div/div/div/div[2]/ciq-performance-details/div/div[1]')))
            screenShot_path =  "\screengrab" + file_system_count + ".png"
            screenshot = driver.save_screenshot(file_path +  screenShot_path)
            time.sleep(1)
            count += 1
            file_system_count = (str(count))
            print(full_url)

或其他任何地方,取决于程序逻辑


推荐阅读