简体   繁体   English

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

[英]How can I make this code execute the if statement properly

The below code is supposed to wait click a button after 2 seconds.下面的代码应该在 2 秒后等待单击按钮。 after two clicks it should wait for 15 seconds before proceeding to click again.单击两次后,它应该等待 15 秒,然后再继续单击。 However the 15 second wait is not happening.然而,15 秒的等待并没有发生。 What am I doing wrong?我究竟做错了什么?

#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)

It seems indentation issue.这似乎是indentation问题。 if else condition falls under except block so until and unless if there is no exception in try block, the code won't execute. if else 条件属于except块,所以直到和除非try块中没有异常,代码将不会执行。

Simple solution would be keep your code with same intend as try except block.简单的解决方案是让您的代码保持与 try 除了块相同的意图。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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