【问题标题】:is there a way to get a bot to automatically restart if the site crashes如果网站崩溃,有没有办法让机器人自动重启
【发布时间】:2021-02-14 17:53:15
【问题描述】:

如果网站崩溃,有没有办法让这个机器人自动重启,如果网站没有正确加载,有没有办法让机器人刷新页面。因为我无法在刷新时获得 xpath,而且我不知道如果机器人没有完成目标,如何让机器人重新启动

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
start_time = time.time()

# my code here



path = "C:\Program Files (x86)\Common Files\Chromedriver.exe"
driver = webdriver.Chrome(path)

# open page
driver.get("https://www.usmint.gov/")
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="navigation"]/div[3]/ul/li[2]/a')) #PRODUCT S
    )
finally:
# product schedule
    driver.find_element_by_xpath('//*[@id="navigation"]/div[3]/ul/li[2]/a').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="navigation"]/div[3]/ul/li[2]/div/div/ul/li[3]/a')) #2020
    )
finally:
# 2020 product schedule
    driver.find_element_by_xpath('//*[@id="navigation"]/div[3]/ul/li[2]/div/div/ul/li[3]/a').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="4cb2318c15eb72316187ca9691"]/div/div/div[2]/div/div[1]/a')) #birth set
    )
finally:
# birth set 2020
    driver.find_element_by_xpath('//*[@id="4cb2318c15eb72316187ca9691"]/div/div/div[2]/div/div[1]/a').click()
    '''
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="emailmodalclose"]')) #clear
    )
finally:
# clear email list stupidity
    driver.find_element_by_xpath('//*[@id="emailmodalclose"]').click()
     '''
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[2]/form/div/div[5]/button[1]')) #add to
    )
finally:
# add to bag
    driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/div[2]/form/div/div[5]/button[1]").click()# double qoutes?
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="mini-cart"]/div[3]/div[2]/div[3]/a')) #checkout
    )
finally:
# checkout
    driver.find_element_by_xpath('//*[@id="mini-cart"]/div[3]/div[2]/div[3]/a').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_login_username"]')) #login
    )
finally:
# login
    driver.find_element_by_xpath('//*[@id="dwfrm_login_username"]').send_keys("email")
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_login_password"]')) #Password
    )
finally:
# password
    driver.find_element_by_xpath('//*[@id="dwfrm_login_password"]').send_keys("password")
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="checkoutMethodLoginSubmit"]/span')) #checkout as
    )
finally:
# checkout as registered user
    driver.find_element_by_xpath('//*[@id="checkoutMethodLoginSubmit"]/span').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]')) #credit card scroll
    )
finally:
# credit card scroll
    driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]').click()  # .format?
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCard_month"]/option[2]')) #cc exp m
    )
finally:
# cc exp month
    driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_month"]/option[2]').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCard_year"]/option[11]')) #cc exp y
    )
finally:
# cc exp year
    driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_year"]/option[11]').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCard_cvn"]')) #cvv
    )
finally:
# cvv
    driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_cvn"]').send_keys("999")
time.sleep(2)
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="checkoutContinuePaymentDelegator"]')) #continue to final
    )
finally:
# continue to final review
    driver.find_element_by_xpath('//*[@id="checkoutContinuePaymentDelegator"]').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="formAgreementLabel"]/span')) #terms of use
    )
finally:
# terms of use button
    driver.find_element_by_xpath('//*[@id="formAgreementLabel"]/span').click()
try:
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="submitOrderButton"]')) #Place order
    )
finally:
# place order
    driver.find_element_by_xpath('//*[@id="submitOrderButton"]').click()
    print ("time elapsed: {:.2f}s".format(time.time() - start_time))
    driver.quit()

#if __name__ == '__main__':
#    order(keys)

如果您在代码中看到任何可以修复的内容,我们将不胜感激

【问题讨论】:

  • 如果“重启机器人”意味着再次运行脚本,只需在代码周围添加一个带有“while True:”的try except,并在完成时将其中断
  • 但我认为这不是您真正的问题,因为您没有在代码中正确使用try except。如果你只是不添加except子句,它在遇到错误时不会做任何事情,包括你想要的“重启”或重新执行
  • 是的,那太好了...你能提供代码吗
  • 如果是真的,你可以尝试:{把所有的东西都放在这里,然后休息} 除了:{print error}。

标签: python selenium bots


【解决方案1】:

根据评论中的要求,这是最简单但不是理想的方式。由于您的try except 子句不包含except 子句,它们在您的代码中没有任何作用,除了它只是抑制您的所有TimeoutException,您实际上需要它们,因为您需要知道它遇到了什么错误。要改进此解决方案,您可以考虑拆分 try except 子句并仅重新执行那些等待元素行而不是整个脚本。

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
start_time = time.time()

# my code here



path = "C:\Program Files (x86)\Common Files\Chromedriver.exe"
driver = webdriver.Chrome(path)

while True:
    try:
        # open page
        driver.get("https://www.usmint.gov/")
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="navigation"]/div[3]/ul/li[2]/a')) #PRODUCT S
        )

        # product schedule
        driver.find_element_by_xpath('//*[@id="navigation"]/div[3]/ul/li[2]/a').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="navigation"]/div[3]/ul/li[2]/div/div/ul/li[3]/a')) #2020
        )

        # 2020 product schedule
        driver.find_element_by_xpath('//*[@id="navigation"]/div[3]/ul/li[2]/div/div/ul/li[3]/a').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="4cb2318c15eb72316187ca9691"]/div/div/div[2]/div/div[1]/a')) #birth set
        )
        # birth set 2020
        driver.find_element_by_xpath('//*[@id="4cb2318c15eb72316187ca9691"]/div/div/div[2]/div/div[1]/a').click()
        '''
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="emailmodalclose"]')) #clear
        )
        # clear email list stupidity
        driver.find_element_by_xpath('//*[@id="emailmodalclose"]').click()
        '''
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[3]/div[2]/div[2]/form/div/div[5]/button[1]')) #add to
        )
        # add to bag
        driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/div[2]/form/div/div[5]/button[1]").click()# double qoutes?
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="mini-cart"]/div[3]/div[2]/div[3]/a')) #checkout
        )
        # checkout
        driver.find_element_by_xpath('//*[@id="mini-cart"]/div[3]/div[2]/div[3]/a').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_login_username"]')) #login
        )
        # login
        driver.find_element_by_xpath('//*[@id="dwfrm_login_username"]').send_keys("email")
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_login_password"]')) #Password
        )
        # password
        driver.find_element_by_xpath('//*[@id="dwfrm_login_password"]').send_keys("password")
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="checkoutMethodLoginSubmit"]/span')) #checkout as
        )
        # checkout as registered user
        driver.find_element_by_xpath('//*[@id="checkoutMethodLoginSubmit"]/span').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]')) #credit card scroll
        )
        # credit card scroll
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCardList"]/option[2]').click()  # .format?
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCard_month"]/option[2]')) #cc exp m
        )
        # cc exp month
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_month"]/option[2]').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCard_year"]/option[11]')) #cc exp y
        )
        # cc exp year
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_year"]/option[11]').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="dwfrm_billing_paymentMethods_creditCard_cvn"]')) #cvv
        )
        # cvv
        driver.find_element_by_xpath('//*[@id="dwfrm_billing_paymentMethods_creditCard_cvn"]').send_keys("999")
        time.sleep(2)
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="checkoutContinuePaymentDelegator"]')) #continue to final
        )
        # continue to final review
        driver.find_element_by_xpath('//*[@id="checkoutContinuePaymentDelegator"]').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="formAgreementLabel"]/span')) #terms of use
        )
        # terms of use button
        driver.find_element_by_xpath('//*[@id="formAgreementLabel"]/span').click()
        element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="submitOrderButton"]')) #Place order
        )
        # place order
        driver.find_element_by_xpath('//*[@id="submitOrderButton"]').click()
        print ("time elapsed: {:.2f}s".format(time.time() - start_time))
        driver.quit()
        break
    except Exception as err:
        print(f"Error: {str(err)}, trying again")
        pass

#if __name__ == '__main__':
#    order(keys)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-04
    • 2020-11-19
    • 2017-12-03
    • 2020-12-31
    • 2021-06-13
    • 1970-01-01
    • 2021-06-30
    • 2013-07-30
    相关资源
    最近更新 更多