重置python脚本中的所有变量(如果术语错误,请更改)
当代码中出现故障时,我希望代码重置并再次运行。我认为我有能力做到这一点,但是当代码在出现故障时重置时,它不会清除x的值。因此,代码将继续执行,因为它停止了对返回开始原因的否定重置python脚本中的所有变量(如果术语错误,请更改),python,loops,Python,Loops,当代码中出现故障时,我希望代码重置并再次运行。我认为我有能力做到这一点,但是当代码在出现故障时重置时,它不会清除x的值。因此,代码将继续执行,因为它停止了对返回开始原因的否定 import time from selenium.webdriver.common.action_chains import ActionChains from selenium.common.exceptions import MoveTargetOutOfBoundsException from selenium i
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import os
import glob
fp = webdriver.FirefoxProfile('directory_on_computer')
browser = webdriver.Firefox(fp)
browser.get('a_website')
time.sleep(8)
browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").clear()
browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").send_keys("user")
browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").clear()
browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").send_keys("pass")
browser.find_element_by_id("ctl00_PlaceHolderMain_login_login").click()
try:
ReportMgr= browser.find_element_by_partial_link_text('Report Manager')
ReportMgr.click()
time.sleep(5)
CustomReport= browser.find_element_by_partial_link_text('Custom Report')
CustomReport.click()
time.sleep(5)
ProgramManagement= browser.find_element_by_partial_link_text('Program Management')
ProgramManagement.click()
ProgramManagement= browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_LEFT)
########testing area below
########Save below
for x in range(1, 71):
browser.find_element_by_partial_link_text('Program Management').click()
time.sleep(120)
browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_DOWN * x, Keys.ENTER)
time.sleep(120)
browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_ImageAnchor > img").click()
time.sleep(120)
browser.find_element_by_partial_link_text('Program Management').click()
time.sleep(120)
except:
pass
如果不需要完全重置,而是需要重置本地自定义变量,请创建函数并使用函数名称空间。然后,只需在循环中调用该函数,并将该循环留作重新启动 2016年3月13日编辑
正确的术语应该是“重启”。不,我不是在开玩笑。最好使用一个循环,并在其中定义所有变量,但是一个快速、简单的解决方案是
execfile(\uu文件)
。(在Python2中)我觉得自己像个白痴……但你会在代码的底部加上“重启”吗?@Yoghatup不,我可能会按照zondo的建议去做。但是,如果你要彻底清理程序的内存空间,那么答案是肯定的:这是通过重新启动来完成的。@konrad rudolph Ok-那么最后一段代码是:重新启动,除了:通过?这听起来像是我想要的——我甚至不在乎这个东西是否继续在无限循环中运行。我只是想确保代码重新开始时所有变量都被重置。如果这样做有效的话,我想给你一些“道具”。将此作为答案提交,我将投票:)因此,最后类似于x=1的内容将变量重置为1?抱歉,我是新手,不知道在哪里可以找到评论。我刚找到它。
#do one-time imports here
def exec_code():
## define local vars at the beginning of the code
counter = 0 ## counter is local to the function and dropped when leaving the func
## arbitrary function code
while True:
counter += 2
print "counter is %i, ending at 10" % counter
answer = raw_input("Restart? (Enter 'y'+return to restart): ")
if answer == "y":
return True ## Return True to exit the function and restart
if counter >= 10:
return False ## Return False or None to exit the loop and do not restart
while exec_code(): pass