简体   繁体   English

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

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

the program should search for a image in the first loop and add it to a list, in the secon loop should he search till he find a image that isnt already in the list.程序应该在第一个循环中搜索图像并将其添加到列表中,在第二个循环中他应该搜索直到找到不在列表中的图像。 PyCharm give me the errors that while search = True: and if pic == used 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)

... ...

I think that you used a bad operator type.我认为您使用了错误的运算符类型。 By that, I mean that you should probably use == and not = .我的意思是你应该使用==而不是= Check it and inform me .检查并通知我

Try this尝试这个

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)

You could also replace search with True and change the “search = False” with “break”您还可以将搜索替换为 True 并将“search = False”更改为“break”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM