[英]is there a way to get a bot to automatically restart if the site crashes
is there a way to get this bot to automaticly restart if the site crashes and is there a way for the bot to refresh the page if the site doesnt load properly.如果站点崩溃,有没有办法让这个机器人自动重新启动,如果站点没有正确加载,是否有办法让机器人刷新页面。 becsause i cant get an xpath on the refresh and i have no clue how to make the bot restart if it didnt accomplish its goals
因为我无法在刷新时获得 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)
and if you see anything in the code that can be fixed it would be appreciated如果您在代码中看到任何可以修复的内容,我们将不胜感激
is there a way to get this bot to automaticly restart if the site crashes and is there a way for the bot to refresh the page if the site doesnt load properly.有没有办法让这个机器人在网站崩溃时自动重启,如果网站没有正确加载,有没有办法让机器人刷新页面。 becsause i cant get an xpath on the refresh and i have no clue how to make the bot restart if it didnt accomplish its goals
因为我无法在刷新时获取xpath,而且我不知道如何在无法完成目标的情况下使bot重新启动
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)
and if you see anything in the code that can be fixed it would be appreciated如果您在代码中看到任何可以修复的内容,我们将不胜感激
As requested in the comment, this is the simplest but not the ideal way.正如评论中所要求的,这是最简单但不是理想的方式。 Since your
try except
clause does not include an except
clause, they serve no purpose in your code except it just suppress all your TimeoutException
, which you actually want them as you need to know what error it encounters.由于您的
try except
子句不包含except
子句,因此它们在您的代码中没有任何作用,除了它只是抑制您的所有TimeoutException
,您实际上需要它们,因为您需要知道它遇到什么错误。 To improve this solution, you can consider to split the try except
clause and re-execute those wait element lines only instead of the whole script.为了改进这个解决方案,您可以考虑拆分
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.