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键退出...") 
相关推荐
小小测试开发6 小时前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想7 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波7 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
copyer_xyf8 小时前
Python 异常处理
前端·后端·python
麻雀飞吧8 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy8 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.9 小时前
【01】Python 机器学习
开发语言·python
为爱停留9 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python
l1t9 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python
曾阿伦10 小时前
Python 搭建简易HTTP服务
开发语言·python·http