gpt4 book ai didi

Python 名称错误 : name 'ElementNotVisibleException' is not defined with Selenium

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:00 25 4
gpt4 key购买 nike

为什么我在使用 Python 和 Selenium 时收到此错误,我该如何解决?

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()

我在网上四处寻找答案,虽然我发现了类似的问题,但我没有找到可以帮助我解决这个问题的答案。

最佳答案

添加下面的 import 语句将避免提到的 NameError

from selenium.common.exceptions import ElementNotVisibleException

关于Python 名称错误 : name 'ElementNotVisibleException' is not defined with Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41209832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com