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

时间:2020-04-10 10:14:56

标签: 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.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)

1 个答案:

答案 0 :(得分:0)

似乎是 indentation 问题。如果其他条件落在except块下,那么直到并且除非try块中没有例外,否则代码将不会执行。

简单的解决方案是使您的代码与try相同,除了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)