简体   繁体   English

如何使用 try 语句优化我的代码?

[英]How do I optimize my code with try statement?

I have a problem that it takes a long time to execute the code that follows the try.我有一个问题,执行 try 之后的代码需要很长时间。 It can be up to 10 seconds.最长可达 10 秒。 Can you think of any optimization?你能想到什么优化吗?

  try:
        error_message = WebDriverWait(self.driver, timeout=0).until(EC.visibility_of_element_located(
            (By.XPATH, "/html/body/div[1]/div[2]/div/div/div[1]/span[2]")))
        assert error_message.text == "Invalid username or password."
        print("3")
        print("Invalid username or password.")
        self.driver.find_element_by_id("username").clear()

    except TimeoutException:
        print(time.time(), "3")

I put the part of code for initializing the web drive.我把初始化网络驱动器的代码部分。 Maybe here is something wrong what do it wait in try long time.也许这里出了点问题,它等待了很长时间。

    def setUp(self):
    p_test = Logintest()
    self.driver = webdriver.Firefox()
    self.driver.get("https://localhost/spcssd")
    print("TC_S_F.IA.AD001_tEST")
    delay = 20  # seconds
    # assert "Log in to spcssd" in self.driver.title
    try:
        element_present = WebDriverWait(self.driver, delay).until(EC.presence_of_element_located((By.ID, "kc-info")))
        print("Page is ready!")
    except TimeoutException:
        print("Loading took too much time!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM