mardi 21 novembre 2017

Selenium find_element_by_css_selector to fire before the page has fully loaded

I have a basic python script to test site page speed. Basically it loads a page, checks an element is visible and then logs the time taken between these two events.

I use this to check that the element is visible:

def wait_for_visibility(selector, timeout_seconds=10):
retries = timeout_seconds
while retries:
    try:
        element = browser.find_element_by_css_selector(selector)
        if element is not None:
            return element
    except:
        if retries <= 0:
            raise
        else:
            pass

    retries = retries - 1
raise Exception(
    "Element %s not visible despite waiting for %s seconds" % (
        selector, timeout_seconds)
)

And I use this order:

    test_times['page_pre_' + name] = time.time()

    browser.get(base + path)

    wait_for_visibility(selector=selector_dict[page], timeout_seconds=240)

    test_times['page_post_' + name] = time.time()

My issue is that the find_element_by_css_selector doesn't seem to finish & return until a few seconds after the page (and more specifically the element it is supposed to look for) is already finished loading. This is really reducing my script's effectiveness as a speedtest.

The page in question is quite large and is built using Vue components, if that is what it causing the issue then are there any alternative ways I can look out for specific pageload events with selenium?

Aucun commentaire:

Enregistrer un commentaire