我为什么会收到使用Python和硒,我该如何解决这个错误定义的?Python的NameError:名字“ElementNotVisibleException”不与硒
NameError: name 'ElementNotVisibleException' is not defined
这本教程http://www.marinamele.com/selenium-tutorial-web-scraping-with-selenium-and-python在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()
我在网上看了看周围的答案时,虽然我已经发现了类似的问题,我的天堂”找到帮助我解决此问题的答案。
错误应该是不言而喻的。你没有定义或导入名为'ElementNotVisibleException'的东西 –
特定的异常不会被导入。 添加''从selenium.common.exceptions导入ElementNotVisibleException''顶部 – Vineesh
@Vineesh谢谢!这工作:)如果你把它作为一个发布,我会接受这个答案。 –