【发布时间】: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