Entering data on hidden elements with selenium (Python 2.7) - Elements are invisible

I am writing a script to log into my bank account using Selenium and Python 2.7. This has historically worked, but now it throws an ElementNotVisibleException. This is my code:

import time
import os 
import subprocess
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui

user_id = raw_input('Enter your account number')
password = raw_input('Enter your password')

driver = webdriver.Chrome()  # Optional argument, if not specified will search path.

def regular():

    site = driver.get('https://www.meridiancu.ca/');

    enter_user_id = driver.find_element_by_xpath('//*[@id="memberNo"]').send_keys(user_id)

regular()

And here is my error message:

Traceback (most recent call last):
  File "C:\Users\Desktop\Programming\Python Files\Monthly\Banking.py", line 50, in <module>
    regular()
  File "C:\Users\Desktop\Programming\Python Files\Monthly\Banking.py", line 19, in regular
    enter_user_id = driver.find_element_by_xpath('//*[@id="memberNo"]').send_keys(user_id)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 349, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: element not visible**
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.16299 x86_64)

The exception is enter_user_id = driver.find_element_by_xpath('//*[@id="memberNo"]')

+4
source share
4 answers

As you tried to identify the Member Number / User ID field with xpathas , this one exactly matches 5 in . Therefore, we need to build a unique or one that uniquely identifies the Member Number / User ID field as follows: //*[@id="memberNo"]xpathHTMLcssxpath

  • xpath

    driver.find_element_by_xpath("//div[@class='homepage-banner-block-grx desktop']//input[@id='memberNo' and @type='text']").send_keys(user_id)
    
  • css_selector

    driver.find_elements_by_css_selector("div.homepage-banner-block-grx.desktop input#memberNo[type=text]").send_keys(user_id)  
    

Note :

  • xpath , / START TO JUMP CONTENT LINK, mobile nav mobile-buttons.

  • , xpath .

  • , send_keys(user_id) , void

0

#, , , , :

, , , ( element.Highlight(), element.Location)

, - , ( )

, , .

,

0

xpath 5 , , "ElementNotVisiableException".

script , . , - , . 5 UserID .

enter_user_id = driver.find_element_by_css_selector('div.desktop input[name="memberNo"]').send_keys(user_id)

, css: div.desktop input[name="memberNo"] DevTool , , . , , css .

0

The problem is that there are 5 tags INPUT, each with an identifier memberNo, but only one of them is visible. What I did in the past in a similar scenario is to get a collection of elements that match this locator and filter the list only for visible ones. You should only get one item in the list, the one you want. It should look something like

memberNos = filter(lambda e: e.is_displayed(), driver.find_elements_by_css_selector("#memberNo"))
memberNos[0].send_keys(user_id)
-1
source

Source: https://habr.com/ru/post/1691956/


All Articles