简体   繁体   中英

Inconsistent selenium.common.exceptions.TimeoutException with Python and Selenium

I am getting an inconsistent TimeoutException when running a Selenium script. If I step through the script using the PyCharm debugger, pausing at each line, the script runs successfully (assert passes, no TimeoutException).

However, when I run the script normally, I get a TimeoutException every time in the last wait.until(). I have read the documentation on EC.presence_of_element_located and I believe I'm using it correctly. Why am I getting this exception? Here's the code:

def test_if_special_diet_types_are_correct(self):
    wait = WebDriverWait(self.driver, 10)
    utils = Utils(self.driver, wait, self)
    utils.login()

    tab = self.driver.find_element_by_id("clients-tab")
    tab.click()

    list_item = wait.until(EC.presence_of_element_located((By.ID, '66')))
    list_item.click()

    edit_button = wait.until(EC.presence_of_element_located((By.ID, 'edit-client-button')))
    edit_button.click()

    element = wait.until(EC.presence_of_element_located((By.ID, 'client-input-first-name')))
    self.assertEqual('John', element.get_attribute('value'))

And here's the error:

Error
Traceback (most recent call last):
  File "/Users/jcleveland/projects/zippymeals/browser_tests/account.py", line 54, in test_if_special_diet_types_are_correct
EC.presence_of_element_located((By.ID, 'client-input-first-name')))
  File "/Users/jcleveland/projects/zippymeals/env/lib/python3.4/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

It would be a guess, but you might need to switch from presence_of_element_located to element_to_be_clickable here:

list_item = wait.until(EC.element_to_be_clickable((By.ID, '66')))
list_item.click()

edit_button = wait.until(EC.element_to_be_clickable((By.ID, 'edit-client-button')))
edit_button.click()

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