【问题标题】:Is there a way to execute a part of a PYTHON script only at a given time有没有办法只在给定时间执行 PYTHON 脚本的一部分
【发布时间】:2021-05-13 15:23:36
【问题描述】:

我编写了一个自动为我注册考试的机器人。 机器人应该比人类快,尤其是最后一行(将在下面添加)

我会在需要执行的时间前几分钟打开 python 脚本。

我正在寻找以下问题的解决方案:

我的代码(在生产中我会将用户名和密码变量更改为非纯文本)。

我也会把它包装在函数中。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from datetime import datetime
import time 

path = "my_path"
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=my_user-data-dir")

driver=webdriver.Chrome(path, chrome_options=options)

time_now = datetime.now()

driver.get("linkt_to_website")


login = driver.find_element_by_class_name("Login")
login.click()

user = driver.find_element_by_id("username")
user.send_keys("my_username")


pwd = driver.find_element_by_id("password")
pwd.send_keys("my_password")


lgnbtn = driver.find_element_by_id("loginbutton")
lgnbtn.click()


driver.get("website linkt to the page with the button to register for my exam")

time.sleep(1)

toggle = driver.execute_script("js function to trigger dropdown menue")

# until here it does not make a difference how much time the bot need 

直到这里一切正常 目标是机器人登录并等待到预定义的时间(小时、分钟、秒、毫秒)

但现在我有一个问题:

# the two lines below are the most important

anmed= driver.find_element_by_xpath("register button xpath ").click()

last_btn= driver.find_element_by_xpath("are you sure register button ").click()

我想在上面两行中触发的两个按钮将在给定的时间出现,所以我需要创建一个函数来触发这两条行,例如早上 10:00:00。

我希望这个问题有意义。

主要目标是在“上午 10:00:00”前几分钟启动脚本,它会登录并等到上午 10 点,并尽可能准确地执行最后两行以实际注册我的考试。

提前谢谢你:)

【问题讨论】:

    标签: python selenium datetime automation bots


    【解决方案1】:

    您确定在按钮变为活动状态时不需要重新加载页面吗?如果不是,这是一种使用时间和日期时间来检查时间是否正确的快速而肮脏的方法,如果不是,请等待(1 秒)然后再试一次。如果您认为这样会更成功,您可以每 0.1 秒检查一次。

    您可能还想使用“尝试”块,以防您的计算机时间和服务器时间之间的同步不一致,因此您寻找一个尚不存在的按钮并触发错误从而破坏您的代码。

    import datetime
    import time
    
    trigger_time = datetime.datetime(2021, 2, 9, 22, 0, 0) # Tonight at 10PM
    
    while True:
        now = datetime.datetime.now()
    
        if now >= trigger_time:
            try:
                # RUN CODE
            except WHATEVEREXCEPTION_SELENEIUM_THROWS_FOR_MISSING_ELEMENTS:
                 pass
            if code_success:
                break
        else:
           time.sleep(1)  # Wait for 1 second
    

    【讨论】:

    • 你会用那个while循环吃掉内存
    • 我运行了 5 分钟,没有任何内存问题..
    【解决方案2】:

    如果您想暂停您的代码直到某个特定时间,那么您基本上希望您的代码休眠 N 秒,其中 N 已被计算为在您想要的时间着陆。

    import time
    
    #Time right now in seconds from epoch. 
    c = time.time() 
    #Time at 6:10 PM today from epoch. 
    n = time.mktime(time.strptime('2021:02:09-18:10:00','%Y:%m:%d-%H:%M:%S'))
    
    #Pause code for seconds until it reaches 6:10 PM today.
    sleep(n-c) 
    
    #Run code after pausing. 
    

    我是 EST 时间,所以对你来说情况可能会有所不同。

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 2019-10-30
      • 2022-01-24
      • 2022-01-27
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      相关资源
      最近更新 更多