硒无法找到元素

时间:2017-10-04 19:26:47

标签: python selenium xpath login inspect

我正在尝试通过Selenium登录网站。我尝试登录的所有网站,我可以做的更少。把这部分代码放到Python 2.7后出现错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 350, in send_keys
    'value': keys_to_typing(value)})
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 499, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061),platform=Mac OS X 10.12.6 x86_64)

代码:

# coding=utf-8
from selenium import webdriver

driver = webdriver.Chrome('/Users/Martin/Desktop/chromedriver')
driver.get("https://car.com")
user= driver.find_element_by_xpath('//*[@id="onevideo_login_username"]')
user.send_keys("email")

检查:

<input type="text" id="onevideo_login_username" name="adaptv_email" data-i18n-placeholder="login.username" ng-model="models.login.email" class="ng-pristine ng-valid" placeholder="Username">

正如我之前所说,我可以登录我正在尝试的所有网站,但这个很难过!

2 个答案:

答案 0 :(得分:0)

  • 网址是否正确?
  • 如果你有id,那么最好使用,
    尝试:

     user=driver.find_element_by_id('onevideo_login_username');
    

答案 1 :(得分:0)

请检查网址,我猜您的网址不正确。首先,我没有在URL中找到这样的任何元素,但根据html我建议您定位元素

<input type="text" id="onevideo_login_username" name="adaptv_email" data-i18n-placeholder="login.username" ng-model="models.login.email" class="ng-pristine ng-valid" placeholder="Username">

使用ID

 user=driver.find_element_by_id('onevideo_login_username');

使用姓名

user=driver.find_element_by_name('adaptv_email');

使用xpath

user= driver.find_element_by_xpath('//*[@name="adaptv_email"]')