1

I'm trying to log onto a webpage with python selenium. I've found an element and it is enabled, but when I try to send_keys() to it I get an error. The main thing (I think) in the error output is

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 

My code is

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import contextlib
with contextlib.closing(webdriver.Firefox()) as driver:
 driver.get('http://www.etoro.com/au')
 elem = driver.find_element_by_class_name('inputUsername')
 print 'enabled:', elem.is_enabled()
 print 'selected:', elem.is_selected()
 elem.send_keys('myusername')

And the output is

enabled: True
selected: False
Traceback (most recent call last):
 File "3_trying_again.py", line 10, in <module>
elem.send_keys('ianafterglow')
 File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 303, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
 File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 385, in _execute
return self._parent.execute(command, params)
 File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
 File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:8959:12)
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11618:15)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11635:11)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11640:7)
at DelayedCommand.prototype.execute/< (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11582:5)

So, what do I need to do?

asked Dec 2, 2014 at 8:09
2
  • Try this elem = driver.find_element_by_class_name('inputUsername') action = webdriver.ActionChains(driver) action.click(elm).perform(); Commented Dec 2, 2014 at 8:20
  • fails on the action.click(elem).perform() line with error selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with Commented Dec 3, 2014 at 6:01

4 Answers 4

2

To make the username field to be visible, you need to move cursor to the login link:

....
driver.get('http://www.etoro.com/au')
action = webdriver.ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath(
 './/a[@class="top-link"]/span[text()="Login"]'
))
action.perform()
# TODO Need to wait until the `inputUsername` field is visible
elem = driver.find_element_by_class_name('inputUsername')
...
answered Dec 2, 2014 at 8:14
Sign up to request clarification or add additional context in comments.

3 Comments

fails on the line './/a[@class="top_link"]/span[text()="Login"]' with error selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":".//a[@class=\"top_link\"]/span[text()=\"Login\"]"}
@user2472657, I cannot reproduce your problem. The code works without the error. FYI, here's the exact code I ran: codepad.org/DfdPxwNG
issue was underscore versus hyphen in top-link. How embarrassing! Thank you.
0

You can use explicit waits:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
 EC.presence_of_element_located((By.CLASSNAME, "inputUsername"))
)
...
answered Dec 2, 2014 at 8:29

1 Comment

the suggested fix code runs successfully, but the program fails in the same way as originally at the elem.send_keys('myusername') line with selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
0

I know that this problem is solved , I got stuck in similar problem and same error
I have fixed it by just make my script sleep for 2 seconds then resume it was just Connection speed problem

...
time.sleep(2)
...

don't forget to import time module

import time

wish that help anyone in future :D

answered May 5, 2016 at 21:17

Comments

0

I had similar issue, selenium was not able to focus and open the login modal. Instead it was focusing on the next element. This was the locator I was using:

elem = browser.find_element_by_xpath("//nav[2]/ul/li[3]/a").click()

I just changed [3] with [2] and it was able to locate the element and open the modal:

elem = browser.find_element_by_xpath("//nav[2]/ul/li[2]/a").click()
answered Sep 9, 2016 at 22:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.