Search code examples
pythonseleniumsys

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


I am trying to make it do a loop while connecting to a webpage via selenium. Here is the code:

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

So how would I call loading() while also connecting to website?


Solution

  • Best thing to do here is parallelism. I know one way, but there may be more efficient ways to do this.

    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