Selenium Webdriver:(python)等待元素不存在(不起作用) [英] Selenium Webdriver: (python) wait for element to not be present (not working)

查看:83
本文介绍了Selenium Webdriver:(python)等待元素不存在(不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Selenium Web驱动程序,并已开始使用python测试网站.我正在等待元素不存在的代码出现问题,因为它似乎正在超时.

I'm learning about the Selenium web drivers and have started using python to test websites. I'm having issues with the wait for element not to be present code, as it seems to be timing out.

这是我到目前为止的代码.

This is the code I have so far.

def test_(self):
        driver = self.driver
        driver.get(self.base_url + "/abc")
        driver.find_element_by_id("UserName").clear()
        driver.find_element_by_id("UserName").send_keys("username")
        driver.find_element_by_id("Password").clear()
        driver.find_element_by_id("Password").send_keys("password")
        driver.find_element_by_id("loginbutton").click()
        for i in range(60):
            try:
                if not self.is_element_present(By.CSS_SELECTOR, ".blockUI"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        driver.find_element_by_xpath(".//*[@id='ChangeTaskGridM']/table/thead/tr/th[1]/a").click()
        for i in range(60):
            try:
                if not self.is_element_present(By.CSS_SELECTOR, ".blockUI"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        driver.find_element_by_xpath(".//*[@id='ChangeTaskGridM']/table/thead/tr/th[2]/a").click()
        for i in range(60):
            try:
                if not self.is_element_present(By.CSS_SELECTOR, ".blockUI"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")

我不确定为什么会超时.如果删除blockUI,它将运行很快,并且我也不想在两者之间放置手动中断.有什么想法吗?

I'm not sure why it's timing out. If I remove the blockUI it will run to fast and I also don't want to put manual breaks in between. Any ideas?

用于检测is_element_present的当前代码

Current code to detect is_element_present

对于范围(60)中的i,
for i in range(60):
            try:
                if not self.is_element_present(By.CSS_SELECTOR, ".blockUI"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")

推荐答案

您应该使用ExpectedConditions来代替检查该元素是否存在的代码.请阅读文档以等待.present_of_element_located()可能是您感兴趣的一种.

Instead of your code to check presence of that element you should use ExpectedConditions. Please read the doc for waits. the presence_of_element_located() is probably the one of interest to you.

这篇关于Selenium Webdriver:(python)等待元素不存在(不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆