TimeOut异常处理

时间:2017-07-11 05:01:25

标签: python-2.7 selenium selenium-webdriver

我通过提供延迟(time.sleep())编写了如下代码,但我希望通过调用try例外功能来修改代码。所以要修改下面的代码。

def min_power(self):
        power_min_list = ["Carrier", "@#$%", "-1", "101", "22"]
        time.sleep(6)
        for i in range(len(power_min_list)):
            if i == 0:
                pencil()
            power_min = web.find_element_by_id("powmin")
            power_min.clear()
            power_min.send_keys(power_min_list[i])
            submit()
            time.sleep(3)
            var1 = web.find_element_by_xpath("//div[@id='grp_powmin']//span[contains(text(),'Number must be between 0 and 100.')]").text
            var2 = "Number must be between 0 and 100."
            if var1 == var2:
                pass
            else:
                print ("Failed - invalid data written successfully")
            if i == 3:
                power_min = web.find_element_by_id("powmin")
                power_min.clear()
                power_min.send_keys(power_min_list[4])
                submit()
        time.sleep(6)
        var = power_min_list[4]
        var3 = web.find_element_by_xpath("//*[@id='modulation_table']/tbody/tr[1]/td[3]").text
        if var == var3:
            pass
        else:
            print ("Failed - Power Min wrong Data is written successfully")

1 个答案:

答案 0 :(得分:0)

您可以使用wait for element而不是time.sleep()

Selenium waitForElement

你不能在方法min_power中删除第二行的睡眠?