password_elem中的硒ElementNotVisibleException

时间:2018-10-19 00:50:35

标签: python-3.x selenium beautifulsoup

我在下面的代码中尝试启动chrome,访问网站并登录。这是基于我在以下回购中找到的代码的基础:

https://github.com/AlbanyCompSci/aeries-api 基本上是为了帮助我登录这个叫做aeries的年级门户网站,并使我的孩子们升入高中。 我收到与“ phantomjs”有关的错误。同样,在抛出关于chrome的错误之前,它必须是无头的。我在较早的文章中获得了帮助: phantomjs issue on mac with jupyter notebook 建议使用chromedriver而不是phantomjs。 现在,我不再像以前的文章中那样从仓库中加载模块,而是使用仓库中的代码片段,并尝试逐步了解它们的工作方式。  我正在使用python 3.6在jupyter笔记本中的Mac上运行它。任何提示,不胜感激。 当我运行下面的代码时,出现与代码的“ password_elem.send_keys(password)”部分相关的“ ElementNotVisibleException”错误。有谁知道可能导致该问题的原因,您能建议如何解决吗?我是硒的新手,因此非常感谢您提出任何建议。

代码:

import pandas as pd
import numpy as np

import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

Options

options = Options()

chrome_options = Options() 
options.add_argument('headless') #downlod Chrome driver.exe 
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)


#system
#JSON for language agnostic output
try:
    import simplejson as json
except ImportError:
    import json


# trying to recreate getGradebooks
# Gradebooks.getGradebooks(session)

#re allows for matching text with regular expressions (including through BeautifulSoup)
#dateutil.parser provies .parse() to convert plain text dates in a variety of formats into datetime objects
import re, dateutil.parser
#BeautifulSoup provide a model for the source HTML
from bs4 import BeautifulSoup

#The default address of the gradebook page for AUSDK12 Aeries
# Drew Gradebook page
ADDRESS = 'https://abip.fsusd.org/ParentPortal/GradebookSummary.aspx'


# recreate session = AeriesSession.Session(email, password)

#Webdriver is interface to the selected browser (PhantomJS)
from selenium import webdriver
#Ability to select values in HTML <select> tags
from selenium.webdriver.support import select
import time

#Base URL of the AUSDK12 Aeries system
BASE_URL=   'https://abip.fsusd.org/ParentPortal/'
#'https://abi.ausdk12.org/aeriesportal/'
#Name of the login page (relative to the base URL)
LOGIN_PAGE =    'LoginParent.aspx'
#<form> id, not currently used
#FORM_ID =      'form1'
#id for email <input>
EMAIL_ID =      'portalAccountUsername'
#id for password <input>
PASSWORD_ID =   'portalAccountPassword'
#id for login button
LOGIN_ID =      'LoginButton'


email=‘madeup@madeup.com’
password=‘madeup’


login_url = BASE_URL + LOGIN_PAGE
#driver = webdriver.PhantomJS(service_log_path='/dev/null')
driver.get(login_url)
email_elem = driver.find_element_by_id(EMAIL_ID)
email_elem.send_keys(email)
password_elem = driver.find_element_by_id(PASSWORD_ID)
password_elem.send_keys(password)
login_elem = driver.find_element_by_id(LOGIN_ID)
login_elem.click()

错误:

---------------------------------------------------------------------------
ElementNotVisibleException                Traceback (most recent call last)
<ipython-input-13-b6872465bdd4> in <module>()
      6 email_elem.send_keys(email)
      7 password_elem = driver.find_element_by_id(PASSWORD_ID)
----> 8 password_elem.send_keys(password)
      9 login_elem = driver.find_element_by_id(LOGIN_ID)
     10 login_elem.click()

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    478                       {'text': "".join(keys_to_typing(value)),
--> 479                        'value': keys_to_typing(value)})
    480 
    481     # RenderedWebElement Items

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    626             params = {}
    627         params['id'] = self._id
--> 628         return self._parent.execute(command, params)
    629 
    630     def find_element(self, by=By.ID, value=None):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    318         response = self.command_executor.execute(driver_command, params)
    319         if response:
--> 320             self.error_handler.check_response(response)
    321             response['value'] = self._unwrap_value(
    322                 response.get('value', None))

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

1 个答案:

答案 0 :(得分:2)

键入电子邮件后,您缺少下一个按钮。

email_elem = driver.find_element_by_id(EMAIL_ID)
email_elem.send_keys(email)

driver.find_element_by_id('next').click() // you are missing this

password_elem = driver.find_element_by_id(PASSWORD_ID)
password_elem.send_keys(password)
login_elem = driver.find_element_by_id(LOGIN_ID)
login_elem.click()