SingSong
SingSong

Reputation: 27

How to fix this issue 'TimeoutException' is not defined in Python Selenium?

Everyone. I hope you are going well. I have one problem to scrape the data from a specific URL.

driver = webdriver.Chrome()
driver.set_window_size(1200, 800)
driver.get(DEGIRO_URL)
loginUsername = driver.find_element_by_xpath("//input[@id='username']")
loginPassword = driver.find_element_by_xpath("//input[@id='password']")
loginBtn = driver.find_element_by_xpath("//button[@type='submit']")
loginUsername.send_keys(LOGIN_USERNAME)
loginPassword.send_keys(LOGIN_PASSWORD)
loginBtn.click()
try:
   myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 
            "//button[@class='duvfw-AB']")))
   myElem.click()
   print ("Page is ready!")
except TimeoutException:
    print ("Loading took too much time!")

When I run this code this shows me the following issues.

DevTools listening on ws://127.0.0.1:51297/devtools/browser/bf64f196-95fe-4df6-8ac8-2761849a317b
[19408:18516:0615/034317.403:ERROR:device_event_log_impl.cc(214)] [03:43:17.403] Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.
Traceback (most recent call last):
  File "E:\Work\scraping\scrape.py", line 83, in main
    myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[@class='duvfw-AB']")))
  File "C:\Users\SingSong\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\Work\scraping\scrape.py", line 147, in <module>
    main()
  File "E:\Work\scraping\scrape.py", line 86, in main
    except TimeoutException:
NameError: name 'TimeoutException' is not defined

If you have experience in this field, Please help me. Best Regards.

Upvotes: 1

Views: 1576

Answers (1)

kennysliding
kennysliding

Reputation: 2977

I am guessing that you have not imported the exception from the selenium module,

simply import it by

from selenium.common.exceptions import TimeoutException

Upvotes: 2

Related Questions