Search code examples
pythonseleniumvisible

Selenium - Element is Not Visible [Python]


I'm currently in CH11 from the "Automate the Boring Stuff with Python" book and I'm going over the Selenium module. I'm trying to move to the end of a page but I'm getting some problems. I also tried to look similar problems in this site and tried the solutions suggested without success unfortunately. Here's my code, when I type it into the IDLE Shell:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser= webdriver.Firefox()
browser.get('http://nostarch.com')
htmlElem= browser.find_element_by_tag_name('html')
type(htmlElem)
<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>
htmlElem.send_keys(Keys.END)  # Error

Exception -:

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    htmlElem.send_keys(Keys.END)
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 347, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not visible

Apparently the html element is not visible? I don't understand how so since it seems to locates the html element just fine as seen on the code without any problems but the Key.ENTER is where I'm getting the error. Any help would be appreciated.


Solution

  • Just tested the following with Chrome driver and it works (It should also work with Firefox):

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    browser = webdriver.Chrome()
    browser.get('http://nostarch.com')
    body_elem = browser.find_element_by_tag_name('body')
    body_elem.send_keys(Keys.END)