简体   繁体   中英

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.

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”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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