Python 如何使此代码正确执行if语句

Python 如何使此代码正确执行if语句,python,selenium,Python,Selenium,下面的代码应该在2秒钟后单击按钮等待。单击两次后,应等待15秒,然后继续再次单击。然而,15秒的等待并没有发生。我做错了什么 #click follow clicked=0 try: Fclick=driver.find_element_by_xpath('//button[text()="Follow"]') driver.execute_script("arguments[0].click();", Fclick) time.s

下面的代码应该在2秒钟后单击按钮等待。单击两次后,应等待15秒,然后继续再次单击。然而,15秒的等待并没有发生。我做错了什么

#click follow
    clicked=0
    try:
        Fclick=driver.find_element_by_xpath('//button[text()="Follow"]')
        driver.execute_script("arguments[0].click();", Fclick)
        time.sleep(1)
        inputElement.send_keys(e)
    except Exception:
        driver.back()
        time.sleep(2)
        driver.get("https://www.instagram.com/am_batman33/?hl=en")
        clicked +=1
        if clicked == 2:
            clicked = 0
            time.sleep(15)
        else:
            time.sleep(2)

似乎是缩进问题。如果else条件属于
除外
块,则除非
中没有异常,否则代码不会执行

简单的解决方案是保持代码与try-except-block相同

clicked=0
try:
    Fclick=driver.find_element_by_xpath('//button[text()="Follow"]')
    driver.execute_script("arguments[0].click();", Fclick)
    time.sleep(1)
    inputElement.send_keys(e)
except Exception:
    driver.back()
    time.sleep(2)
    driver.get("https://www.instagram.com/am_batman33/?hl=en")
    clicked +=1

if clicked == 2:
    clicked = 0
    time.sleep(15)
else:
    time.sleep(2)

下面的答案是否解决了您的问题?