简体   繁体   中英

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. It can be up to 10 seconds. 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!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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