簡體   English   中英

完成后如何 go 回到循環頂部?

[英]How to go back to top of loop after finishing?

使用 selenium 訪問某些網站時,有時頁面無法正確加載。 所以我想編寫一個代碼,通過搜索特定按鈕來檢查網站是否正確加載。 如果找不到按鈕,則需要刷新直到找到按鈕,如果代碼確實找到了按鈕,則需要執行代碼的 rest。

例如:按鈕不存在:>>>刷新>>>(再次檢查按鈕是否不存在)按鈕不存在>>>刷新>>>(再次檢查按鈕是否不存在)按鈕存在>>> rest代碼

我目前擁有的循環看起來像這樣,但刷新循環后不會重新啟動並運行 else:function。 所以問題是我如何制作一個循環,在它刷新后重新啟動循環。

while not (driver.find_elements(By.CLASS_NAME, "button")):
    driver.refresh()
else: 
    Rest of code

幫助將不勝感激,在此先感謝

您可以有一個無限的 while 循環和一個帶有find_elements的 if 條件,請注意find_elements不會返回任何異常,它會返回 web 元素列表或 0。

代碼:

while True:
    try:
        if len(driver.find_elements(By.XPATH, "xpath of the button")) >0:
            print("Button is present, since find elements list is non empty")
            # execute the code, may be click on the button or whatever it is.
            #make sure to exit from infinite loop as well
            break
        else:
            driver.refresh()
            #may be put some delay here to let button available to click again.
    except:
        print("There was some problem trying to find the button element, tearing it down")
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM