当 selenium 连接到网页时,我将如何进行循环?

How would I do a loop while selenium connects to a webpage?

我试图让它在通过 selenium 连接到网页时循环。这是代码:

from sys.stdout import flush
from sys import argv
from selenium import webdriver
def loading():
    print("Loading.\r")
    flush()
    print("Loading..\r")
    flush()
    print("Loading...\r")
    flush()
driver = mydriverslocation
website = argv[1]
driver.get(website)
# Do loading() while connecting to website

那么我如何在连接到网站的同时调用 loading()

这里最好的办法是并行。我知道一种方法,但可能还有更有效的方法。

import threading
from sys.stdout import flush
from sys import argv
from selenium import webdriver

website_loaded = False

def loading():
    while not website_loaded:
        print("Loading.\r")
        flush()
        print("Loading..\r")
        flush()
        print("Loading...\r")
        flush()

driver = mydriverslocation
website = argv[1]

# Start the loading thread
# Expected method to run, and arguments.
loading_thread = threading.Thread(loading, ())
loading_thread.start()

driver.get(website)
# And any other code while loading

# This will pass to the thread
website_loaded = True