我爲什麼會收到使用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謝謝!這工作:)如果你把它作爲一個發佈,我會接受這個答案。 –