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键退出...") 
相关推荐
lie..3 分钟前
基于大模型的智能客服系统部署与使用(二):接入前端可视化界面
人工智能·python
tedcloud1233 分钟前
Understand-Anything部署教程:打造AI代码理解平台
服务器·人工智能·学习·自动化·powerpoint
光影6278 分钟前
Python接口自动化测试----Requests库基础入门
开发语言·python·测试工具·pycharm·自动化
程序媛_8 分钟前
【Python】连接PostgreSQL获取手机验证码
开发语言·python·postgresql
Kobebryant-Manba12 分钟前
学习参数管理
pytorch·python·深度学习
是有头发的程序猿13 分钟前
竞品分析 + 用户洞察自动化|基于 item_review 评论接口 + 多 AI Agent 实现淘宝评论全量采集与智能分析(附python源码)
java·python·自动化
信看13 分钟前
Jetson Orin Quectel QMI 拨号上网
开发语言·python
骑士雄师23 分钟前
课程导航LangGraph核心概念
python
SilentSamsara27 分钟前
DuckDB + Python:嵌入式 OLAP 数据库的轻量分析实战
开发语言·数据库·python·微服务
爱上纯净的蓝天34 分钟前
30 分钟上手 AtomCode:用它写一个 Python 批量整理文件/改名/生成报告小工具(新手教程)
python·开源·自动化脚本·atomcode·ai 编码助手