0

I'm using a bit of code from a related question

username = driver.find_element_by_name("username")

username.send_keys("username")

I looked at the source code for the login page, and saw: HTML snippet

However, I'm getting errors for both username = driver.find_element_by_name("Email") and username = driver.find_element_by_name("ember442")

The error says:

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 385, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
    'value': value})['value']
  File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"Email"}

What am I doing wrong here? My only other thought would be to use one of the other commands for locating elements (find_element_by_id, find_element_by_name, find_element_by_xpath, find_element_by_link_text, find_element_by_partial_link_text, find_element_by_tag_name, find_element_by_class_name, find_element_by_css_selector) but those don't seem to work either.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
willyd
  • 1
  • 3
  • What you're doing wrong is you're not learning how to use selenium. You're just copying and pasting code. You use `find_element_by_name` if the code you're searching for has a name. Does your html element have a name? You have to ask yourself what is unique about the thing you are searching for - name? id? link text? Figure that out, then read the documentation to find a method that will let you search for that unique bit of information. – Bryan Oakley Jun 23 '17 at 15:31
  • I thought that's what "placeholder=____" meant.. Thanks for your answer though, I'll give that a try – willyd Jun 23 '17 at 15:33
  • Got it! After like 1.5 hours... from a related Java question [link](https://stackoverflow.com/questions/34722895/java-selenium-webdriver-cant-find-form-field) I found a bit of code: `driver.findElement(By.xpath("//input[@name='name']"));` and modified it to read `username = driver.find_element_by_xpath("//input[@placeholder='Email']")` Thanks for all your responses. I definitely am "copying and pasting code" here... but I am trying to do it in a semi-intelligent way, and right now that's me doing my best to learn... – willyd Jun 23 '17 at 16:11

3 Answers3

1

It's because none of those are element names. You should use :

username = driver.find_element_by_id("ember442")
MikeJRamsey56
  • 2,779
  • 1
  • 20
  • 34
HH1
  • 598
  • 1
  • 6
  • 13
  • I tried exactly that, and got a similar error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"ember442"} – willyd Jun 23 '17 at 15:35
  • Try [waiting](https://selenium-python.readthedocs.io/waits.html) for the element to exist in the DOM. – MikeJRamsey56 Jun 23 '17 at 16:19
0

Here is the Answer to your Question:

For Email try xpath as //input[@id='ember442']:

username = driver.find_element_by_xpath("//input[@id='ember442']")
username.send_keys("username")

For Password try xpath as //input[@id='ember443']:

username = driver.find_element_by_xpath("//input[@id='ember443']")
username.send_keys("username")

Let me know if this Answers your Question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • why do you recommend `find_element_by_xpath` if you have an id? Why not `find_element_by_id`? – Bryan Oakley Jun 23 '17 at 15:32
  • Tried that, also got an error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id='ember442']"} – willyd Jun 23 '17 at 15:36
  • @ BryanOakley It is often observed that though an element, likes the one in our discussion seems to have an unique `id`, but in somecases when the `id` is with in `input` or `span` tag Selenium fails to find it while polling the HTML DOM. So to make the search more easier we try to pass on the intended `tag` name to the `xpath` to narrow down the search. Thanks – undetected Selenium Jun 23 '17 at 15:46
  • @willyd Check 2 things. 1) Check if the form is within an `iframe`, in that case you have to switch to the frame first then locate the elements. 2. If the page contains JavaScripts or AJAX calls induce some `ExplicitWait` for the `input` elements to be `clickable` first. Update me the status. Thanks – undetected Selenium Jun 23 '17 at 15:55
  • figured it out, see comment on original post. Thanks! – willyd Jun 23 '17 at 16:11
0

Got it! I ended up finding a related question (albeit one using Java) and a piece of code in an answer: driver.findElement(By.xpath("//input[@name='name']")) and modified it to read: username = driver.find_element_by_xpath("//input[@placeholder='Email']"‌​) I'd love to know how to do this by referencing the id name (find_element_by_id) but this was a task-specific question so it'll work for now!

TLDR; I had to specify the placeholder name within find_element_by_xpath.

willyd
  • 1
  • 3
  • if the element has an id, it should work. All things considered, searching by id is the most reliable assuming your ids are unique. – Bryan Oakley Jun 23 '17 at 16:28