导入错误:无法从"selenium. webdriver. support. ui"导入名称"expected_conditions"

v09wglhw  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(219)

我正在尝试使用Selenium和Python运行一个Web Scraper。我已经导入了必要的模块并设置了ChromeDriver可执行文件的路径,但当我尝试运行脚本时,我一直得到以下错误:"导入错误:无法从"selenium. webdriver. support. ui"导入名称"expected_conditions""。
我已经尝试卸载并重新安装selenium模块,但错误仍然存在。我还检查了以确保我使用的是最新版本的ChromeDriver。
我使用的工具,如curl,wget,httrack,但似乎没有工作安装通过登录页面,因为我的网站有一个javascript登录提示。我是开放的新工具,如果你有建议。
有人能帮我找出问题出在哪里吗?我对网页抓取或调试不是很熟悉,所以任何建议都将不胜感激。
我已经按照教程编写了以下代码:

import os
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

# Set the path to the ChromeDriver executable
chrome_driver_path = r"C:\chromedriver\chromedriver.exe"

# Initialize a Chrome webdriver instance
driver = webdriver.Chrome(chrome_driver_path)

# Navigate to the login page
driver.get("https://lms.infowerk.at/mysite")
print(driver.page_source)

# Wait for the username field to be present in the DOM
WebDriverWait(driver, 10).until(
    lambda driver: driver.find_element(By.NAME, "username")
)

# Find the login form elements and enter the login credentials
driver.find_element(By.NAME, "Username").send_keys("denis")
driver.find_element(By.NAME, "Password").send_keys("thebestpassword")

# Submit the login form
driver.find_element(By.XPATH, "//button[@type='submit']").click()

# Wait for the page to load
time.sleep(5)

# Create a directory to store the page HTML files if it doesn't already exist
if not os.path.exists("D:/mysite"):
    os.makedirs("D:/mysite")

# Save the current URL for later use
current_url = driver.current_url

# Save the HTML of the current page to a file
with open("D:/mysite/index.html", "w") as f:
    f.write(driver.page_source)

# Find all of the links on the page
links = driver.find_elements(By.XPATH, "//a")

# Iterate over the links
for link in links:
    href = link.get_attribute("href")
    if href.startswith(current_url):
        driver.get(href)
        with open("D:/mysite/" + href.split("/")[-1], "w") as f:
            f.write(driver.page_source)

这是我收到的错误消息"

ImportError: cannot import name 'expected_conditions' from 'selenium.webdriver.support.ui'
fjnneemd

fjnneemd1#

您还必须添加以下import语句:

from selenium.webdriver.support import expected_conditions

相关问题