-3

having trouble with my python code. I keep getting python no such element: unable to locate element {"method": "id","selector":"email"}

my code:

self.driver.get(redirecturl)

email = "testmail02015@
Password = "Passw0rd123"
emailFieldID = "email"
passwordFieldID = "password"
loginButtonXpath = "//button[@value='btnLogin']"

self.driver.find_element_by_id(emailFieldID).send_keys(email)
self.driver.find_element_by_id(passwordFieldID).send_keys(Password)
self.driver.find_element_by_xpath(loginButtonXpath).click()
Nazim Kerimbekov
  • 4,497
  • 8
  • 31
  • 54
Tony sanchez
  • 85
  • 1
  • 2
  • 9

1 Answers1

3

Usually the problem is that find_element runs too quickly before page fully loaded. So try to wait for elements to appear (in the example it waits for maximum of 10 seconds; less if element appears earlier):

...
emailFieldID = "email"
...

WebDriverWait(browser, 10).until(EC.presence_of_element_located(browser.find_element_by_id(emailFieldID)))

self.driver.find_element_by_id(emailFieldID).send_keys(email)

After that you can use find_element as usual.

ohglstr
  • 10,086
  • 8
  • 41
  • 63
  • what part of the script do i add this too? sorry – Tony sanchez Aug 03 '16 at 17:29
  • `WebDriverWait.until()` returns the element waited for. You can just capture that and `.send_keys()` to it. There's no need to scrape the page again. – JeffC Aug 04 '16 at 02:21
  • @JeffC was trying to emphasize that waiting for `emailFieldID` was an arbitrary choice, nothing to do with the fact that the same field is operated upon. – ohglstr Aug 04 '16 at 14:23
  • What should I import to use this line? – mm_ Mar 02 '19 at 19:29