1
<input placeholder="Enter Password" class="password-input" type="password">

Above is the element I am trying to send keys to using the below code.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')

driver.get('https://howsecureismypassword.net/')

elem = driver.find_element_by_name('password-input')
elem.send_keys('password')

html = driver.page_source

This is the error I get and can't figure out why.

 RESTART: C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\Science 

Experiment\Science_Experiment_Password_Tester.py 
Traceback (most recent call last):
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\Science Experiment\Science_Experiment_Password_Tester.py", line 18, in <module>
    elem = driver.find_element_by_name('password-input')
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
    'value': value})['value']
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"password-input"}
  (Session info: chrome=57.0.2987.133)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)

This is the site - https://howsecureismypassword.net/

1 Answer 1

1

Try find_element_by_class_name instead of find_element_by_name.

Looking at the HTML of your site, the class name of the element you want is password-input, not the name.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')

driver.get('https://howsecureismypassword.net/')

elem = driver.find_element_by_class_name('password-input')
elem.send_keys('password')

html = driver.page_source
Sign up to request clarification or add additional context in comments.

3 Comments

I still get the same error but with this changed --- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"password-input"}
Maybe try putting a wait in between driver.get and the find_element line. Sometimes selenium moves too fast for it's own good, and tries to find an element on a partially loaded page. Try importing time then putting the line time.sleep(5) in between driver.get and elem=driver.find_elem... That's an extremely long wait, but it should show if that's the problem.
You were right Christopher Apple it works now - Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.