Home > front end >  How to make a loop that keeps cheking if theres a certain class
How to make a loop that keeps cheking if theres a certain class

Time:12-28

Hello I'm trying to make a loop that keeps searching for a class with "buy" and if it finds it needs to refresh until it can't find it anymore and then it runs the rest of the code. I'm using selenium and python help would be much appreciated. Thanks in advance

foundButton = False
    while not foundButton:
        driver.find_element(By.CLASS_NAME, "buy")    
        if (driver.find_element(By.CLASS_NAME, "buy")):
            time.sleep(1)
            driver.refresh()
            driver.find_element(By.CLASS_NAME, "buy")
        else:
            foundButton = True

CodePudding user response:

While not true is the actual loop condition. The value must be true.

while(condition) or if(condition) implies do the action if condition evaluates to true. while not means do it if the condition is false. but because your initial value is false, the not negates it to true and ultimately the loop bever executes because the falseButton is false and the loop condition interprest as true always.

I always advise against using boolean values to control loop conditions because especially in python it is easy to confuse outer logic with inner logic. What I mean is that

while (condition)
while [not (condition)]

No matter what the outcome is, for the while to proceed, the cumulative outcome must be true always.

  • Related