Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why the very long wait in wait_until_completion( ) #70

Open
LambertWM opened this issue Jun 10, 2023 · 2 comments
Open

why the very long wait in wait_until_completion( ) #70

LambertWM opened this issue Jun 10, 2023 · 2 comments

Comments

@LambertWM
Copy link

I found that we spend 90% of the time in wait_until_completion( ), because the delay value time.sleep(randint(3, 5)) is 3 to 5 seconds, which seems very high - why is that?

time.sleep(random.uniform(0.1, 0.2)) seems more than enough for my simple tests, but maybe I'm missing something?

@LambertWM
Copy link
Author

I spoke too soon - in order for the scrolling and fetching more tweets to work, a small delay has to be added to the scroll function as well:

def scroll_down(driver) -> None:
    """Helps to scroll down web page"""
    try:
        start = time.time()
        body = driver.find_element(By.CSS_SELECTOR, 'body')
        for _ in range(randint(2, 4)):
            body.send_keys(Keys.PAGE_DOWN)
            time.sleep(random.uniform(0.2, 0.3))
        print("scroll_down took " + str(time.time()-start));
    except Exception as ex:
        logger.exception("Error at scroll_down method {}".format(ex))

@staticmethod
def wait_until_completion(driver) -> None:
    """waits until the page have completed loading"""
    try:
        state = ""
        start = time.time()
        while state != "complete":
            time.sleep(random.uniform(0.1, 0.2))
            state = driver.execute_script("return document.readyState")
        print("wait_until_completion() took " + str(time.time()-start));
    except Exception as ex:
        logger.exception('Error at wait_until_completion: {}'.format(ex))

@LambertWM
Copy link
Author

this leads me to believe that wait_until_completion( ) doesn't really do what it suggests. An alternative strategy, which has worked for me in the past, could be to send a PAGE_DOWN, then wait a little bit and to keep doing this as long until the document height has changed more than a certain amount (or a time out is reached).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant