Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-Selenium won't wait for my website to refresh?

I am trying to test that logged in users can logout.

To tell Selenium I am logged in I cookie-jack the sessionid, like so:

@step(r'I am logged in as "(\w*)"')
def log_in(step, name):
    can_login = world.client.login(username=name, password=name)
    if can_login:
        session_key = world.client.cookies['sessionid'].value
        world.browser.add_cookie({'name':'sessionid', 'value':session_key})
        world.browser.get(world.browser.current_url)
        from time import sleep    #Really should have to do this
        sleep(100)
    else: 
        raise Exception("Could not login with those credentials")

I need to refresh for the html to change on 'login', but selenium is taking so long to refresh the page (it's on localhost, which I know can have problems). I do have an implicit wait in my terrain.py:

world.browser.implicitly_wait(10)

But I don't think it's taking effect. How can I tell selenium to wait for the page to load each time?

like image 993
AncientSwordRage Avatar asked Feb 24 '26 12:02

AncientSwordRage


1 Answers

Implicit wait would not help since it is just telling how long to wait while finding an element.

Instead, you need to explicitly wait for an element appearing after the page load:

world.browser.get(world.browser.current_url)

element = WebDriverWait(world.browser, 50).until(
    EC.presence_of_element_located((By.ID, "header"))
)

This would tell selenium to wait for at most 50 seconds, checking element existence every 500 milliseconds, think about it as polling.

like image 138
alecxe Avatar answered Feb 26 '26 01:02

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!