Python-Selenium不会等待我的网站刷新?

时间:2015-01-19 14:07:06

标签: python selenium selenium-webdriver

我正在尝试测试已登录的用户是否可以注销。

告诉Selenium我已登录I cookie-jack the sessionid,如下所示:

@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")

我需要刷新html以更改登录',但是selenium需要很长时间来刷新页面(它在localhost上,我知道可能有问题)。我在terrain.py中有一个隐含的等待:

world.browser.implicitly_wait(10)

但我认为它没有生效。如何告诉selenium每次都要等待页面加载?

1 个答案:

答案 0 :(得分:2)

隐式等待无济于事,因为它只是告诉我们在找到元素时要等多久。

相反,对于页面加载后出现的元素,您需要explicitly wait

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

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

这会告诉selenium最多等待50秒,每500毫秒检查一次元素是否存在,将其视为轮询。