Python自动化selenium-一直卡着不打开浏览器怎么办?

Python自动化selenium

如果出现卡住不打开,就把驱动放当前目录并指定

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import os

def open_baidu():
    # 获取当前目录中的chromedriver.exe的绝对路径
    current_dir = os.path.dirname(os.path.abspath(__file__))
    chromedriver_path = os.path.join(current_dir, "chromedriver.exe")
    
    # 设置Chrome浏览器选项
    chrome_options = webdriver.ChromeOptions()
    
    # 使用当前目录中的chromedriver.exe
    service = Service(executable_path=chromedriver_path)
    
    # 初始化Chrome浏览器
    print("正在初始化Chrome浏览器...")
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    # 打开百度网站
    print("正在打开百度网站...")
    driver.get("https://www.baidu.com")
    
    # 最大化浏览器窗口
    driver.maximize_window()
    
    # 等待10秒,以便查看页面
    print("成功打开百度首页")
    time.sleep(10)
    
    # 关闭浏览器
    driver.quit()
    print("浏览器已关闭")

if __name__ == "__main__":
    try:
        open_baidu()
    except Exception as e:
        print(f"发生错误: {e}")
        input("按Enter键退出...") 
相关推荐
码云骑士1 分钟前
99-混合搜索Hybrid-Search-BM25-稀疏稠密向量-融合排序
python
金銀銅鐵19 分钟前
[Python] 借助 Turtle 逐字展示《醉翁亭记》
python
lushanzheng25 分钟前
2026年必应快排程序:基于 Selenium 自动化解决方案
selenium·测试工具·自动化
mengpp_12345633 分钟前
物联网云平台可以离线使用吗?摒弃云端依赖,工业现场离线自动化解决方案
运维·物联网·自动化·华为云
xiaoxiangsiyan1 小时前
企业网络运维自动化与管理协议深度指南
linux·运维·服务器·网络·学习·自动化
用户7088769039381 小时前
给传统 SaaS 增加 AI 能力:3个真实落地场景
python
卷无止境1 小时前
FastAPI中间件全解析:请求处理链条上的隐形关卡
后端·python·fastapi
用户0332126663671 小时前
使用 Python 将 HTML 内容添加到 PowerPoint 演示文稿中
python·html
看浪的路人1 小时前
第4讲:MCP Client 开发——连接、发现、调用
windows·python·ai
Swift社区1 小时前
Python 开发环境怎么选?PyCharm、VS Code、Trae 谁更适合 AI 开发?
人工智能·python·pycharm