为什么我的嵌套 while 循环不起作用? if 和第二个 while 循环是预期的

Why is my nested while loop not working? if and second while loop is expected

程序应该在第一个循环中搜索图像并将其添加到列表中,在第二个循环中他应该搜索直到找到列表中不存在的图像。 PyCharm 给我 while search = True: and if pic == used 预期的错误。

used = []
search = True

#First Loop
while True:
    pic = driver.find_element(By.CSS_SELECTOR,value=".image-post img")
    time.sleep(2)
    pic_url = pic.get_attribute("src")
    pic_title = pic.get_attribute("alt")
    used.append(pic)
    time.sleep(200)

#Second loop
        while search = True:
        pic = driver.find_element(By.CSS_SELECTOR, value=".image-post img")
        if pic == used
            search = True

        else:
            search = False

    used.append(pic)

...

我认为您使用了错误的运算符类型。也就是说,我的意思是您应该使用 == 而不是 =查看后通知我.

试试这个

used = []

#First Loop
while True:
    search = True #moved search = True here so that the nested loop will run each iteration of the main loop as it will be set back to True
    pic = driver.find_element(By.CSS_SELECTOR,value=".image-post img")
    time.sleep(2)
    pic_url = pic.get_attribute("src") #you’re not doing anything with this from the looks of it
    pic_title = pic.get_attribute("alt") #same with this
    used.append(pic)
    time.sleep(200)

#Second loop
        while search:
            pic = driver.find_element(By.CSS_SELECTOR, value=".image-post img")
            if pic != used:#may want to change “used” to “used[-1]” here as used is a list while it appears pic is not, so the -1 will get the last value in used
                search = False

    used.append(pic)

您也可以将 search 替换为 True,并将“search = False”更改为“break”