Home > Software design >  Why is my nested while loop not working? if and second while loop is expected
Why is my nested while loop not working? if and second while loop is expected

Time:05-27

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)

...

CodePudding user response:

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.

CodePudding user response:

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”

  • Related