2one2 2one2 - 5 months ago 15
Python Question

raise TimeoutException(message, screen, stacktrace) TimeoutException: Message:

im new to python and selenium in general and i was trying an example i saw in youtube. this is the code example:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import unittest

class LoginTest(unittest.TestCase):

def test_Login(self):
self.driver = webdriver.Firefox()
self.driver.get("https://www.facebook.com/")
driver = self.driver
facebookUsername = "xxxxxxxx"
facebookPassword = "xxxxxxxx"

emailFieldId="email"
passFieldId ="pass"
loginButtonXpath="//input[@value='Log in']"
fbLogoXpath = "(//a[contains(@href,'logo')])[1]"

emailFieldElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(emailFieldId))
passFieldElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(passFieldId))
loginButtonElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(loginButtonXpath))

emailFieldElement.clear()
emailFieldElement.send_keys(facebookUsername)
passFieldElement.clear()
passFieldElement.send_keys(facebookPassword)
loginButtonElement.click()
WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(fbLogoXpath))
def tearDown(self):
self.driver.quit()

if __name__ == "__main__":
unittest.main()


when i run the code it enters to facebook, but stops and gives me this error.

ERROR: test_Login (__main__.LoginTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "facebot.py", line 25, in test_Login
loginButtonElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(loginButtonXpath))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 76, in until
raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriv
er@googlecode.com/components/driver-component.js:10667)
at FirefoxDriver.prototype.findElement (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriver@google
code.com/components/driver-component.js:10676)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdrive
r@googlecode.com/components/command-processor.js:12643)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriver@
googlecode.com/components/command-processor.js:12648)
at DelayedCommand.prototype.execute/< (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriver@googlec
ode.com/components/command-processor.js:12590)

----------------------------------------------------------------------
Ran 1 test in 16.262s


i have tried fixing it by looking at youtube, but im stuck and dont know what could it be.

Answer

For starters, the timeout value is too small, increase it. Plus, you can define the WebDriverWait instance only once and reuse:

wait = WebDriverWait(driver, 10)

emailFieldElement = wait.until(lambda driver: driver.find_element_by_id(emailFieldId))
passFieldElement = wait.until(lambda driver: driver.find_element_by_id(passFieldId))
loginButtonElement = wait.until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))

Also, note that you should use find_element_by_xpath() instead of find_element_by_id() when locating the "Log In" button.

Comments