找不到元素selenium python

时间:2017-04-18 23:04:53

标签: python windows selenium selenium-chromedriver

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

以上是我尝试使用以下代码发送密钥的元素。

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

这是我得到的错误,无法弄清楚原因。

 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)

这是网站 - https://howsecureismypassword.net/

1 个答案:

答案 0 :(得分:1)

尝试使用find_element_by_class_name代替find_element_by_name

查看您网站的HTML,您想要的元素的类名password-input,而不是名称。

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