[英]Why am I getting StaleElementReferenceException even though the element is present in my loop? (Selenium Python)
[英]Why does my counter not update even though I am adding one on every loop?
出於某種原因,即使我在 while 循環中添加了一個計數器,我的計數器也沒有更新?
代碼:
counter = 1
def loo(counter):
counter+=1
return counter
while 1:
print(loo(counter))
發生這種情況是因為函數內的計數器變量是局部的,而不是全局的。 因此它只會在函數內部更新。 但是,如果您將函數的值分配給全局計數器,您將實現您想要的。
glob_counter = 1
def loo(local_counter):
local_counter += 1
return local_counter
while 1:
glob_counter = loo(glob_counter)
print(glob_counter)
當您將計數器作為函數的參數傳遞時,您將創建一個新實例,因此原始變量計數器不會更新並且其值保持為 1。
改為這樣做:
counter = 1
def loo(counter):
counter+=1
return counter
while 1:
counter = loo(counter)
print(counter)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.