5

Why am I receiving this error using Python and Selenium and how can I fix it?

NameError: name 'ElementNotVisibleException' is not defined

It occurs when running the following script from this tutorial http://www.marinamele.com/selenium-tutorial-web-scraping-with-selenium-and-python in Python3.5

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver


def lookup(driver, query):
    driver.get("http://www.google.com")
    try:
        box = driver.wait.until(EC.presence_of_element_located(
            (By.NAME, "q")))
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.NAME, "btnK")))
        box.send_keys(query)
        try:
            button.click()
        except ElementNotVisibleException:
            button = driver.wait.until(EC.visibility_of_element_located(
                (By.NAME, "btnG")))
            button.click()
    except TimeoutException:
        print("Box or Button not found in google.com")


if __name__ == "__main__":
    driver = init_driver()
    lookup(driver, "Selenium")
    time.sleep(5)
    driver.quit()

I've looked around the web for answers and while I've found similar questions, I haven't found answers that have helped me resolve this issue.

5
  • The error should be self explanatory. Nowhere have you defined or imported something named ElementNotVisibleException Commented Dec 18, 2016 at 15:28
  • 3
    Specific exception is not imported. Add from selenium.common.exceptions import ElementNotVisibleException on the top Commented Dec 18, 2016 at 15:32
  • @Vineesh Thanks! That worked :) I will accept that as an answer if you post it as one. Commented Dec 18, 2016 at 15:38
  • @BryanOakley Thanks, I assumed that it wasn't defined but wasn't sure why since ElementNotVisibleException is included in selenium.common.exceptions Commented Dec 18, 2016 at 15:40
  • sure, I will post in answer block Commented Dec 18, 2016 at 15:49

1 Answer 1

14

Adding below import statement will avoid the mentioned NameError

from selenium.common.exceptions import ElementNotVisibleException
Sign up to request clarification or add additional context in comments.

1 Comment

Also note, as mentioned by @BrianOakley: Nowhere have you defined or imported something named ElementNotVisibleException - this is why the error is produced.

Your Answer

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.