[英]Raise exception only at the end of for loop in Python Selenium
[英]Selenium Python - How to raise an exception and continue the loop?
如果發現任何不匹配,我想拋出異常錯誤,但循環應該繼續。 如果有任何不匹配/異常錯誤,整個案例都應該失敗。 你們都可以檢查下面的代碼並在這里幫助我嗎?
def test01_check_urls(self, test_setup):
#reading the file
Total_entries=len(old_urls) //Total_entries=5
print("Total entries in the sheet: "+ str(Total_entries))
col_count=0
#opening urls
while col_count<Total_entries:
Webpage=old_urls[col_count] //fetching data from 1st cell in the excel
Newpage=new_urls[col_count] //fetching data from 1st cell in the excel
driver.get(Webpage)
print("The old page url is: "+Webpage)
page_title=driver.title
print(page_title)
Redr_page=driver.current_url
print("The new url is: "+Redr_page)
print("New_url from sheet:"+Newpage)
try:
if Redr_page==Newpage:
print("Correct url")
except:
raise Exception("Url mismatch")
col_count+=1
有一個變量url_mismatch
,最初是False
。 當 URL 不匹配時,不要立即引發異常,只需將此變量設置為True
。 然后當循環結束時,檢查該變量的值,如果該變量為True
則引發異常。
但是,尚不清楚您的try
塊如何導致異常。 您可能是說(不需要try
塊):
if Redr_page == Newpage:
print("Correct url")
else:
raise Exception("Url mismatch")
現在我不修改那部分代碼:
url_mismatch = False
while col_count<Total_entries:
Webpage=old_urls[col_count] //fetching data from 1st cell in the excel
Newpage=new_urls[col_count] //fetching data from 1st cell in the excel
driver.get(Webpage)
print("The old page url is: "+Webpage)
page_title=driver.title
print(page_title)
Redr_page=driver.current_url
print("The new url is: "+Redr_page)
print("New_url from sheet:"+Newpage)
try:
if Redr_page==Newpage:
print("Correct url")
except:
print('Mismatch url')
url_mismatch = True # show we have had a mismtach
col_count+=1
# now check for a mismatch and raise an exception if there has been one:
if url_mismatch:
raise Exception("Url mismatch")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.