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 分钟前
建模步骤 3 :数据探索(EDA) — 1、初步了解数据:常用函数
人工智能·python·机器学习·数据挖掘·数据分析·pandas·数据可视化
Chen--Xing7 分钟前
2025鹏城杯 -- Crypto -- RandomAudit详解
python·密码学·ctf·鹏城杯
一瞬祈望30 分钟前
PyTorch 图像分类完整项目模板实战
人工智能·pytorch·python·深度学习·分类
坐吃山猪36 分钟前
BrowserUse12-源码-MCP模块
python·llm·playwright·browser-use
昔时扬尘处40 分钟前
【Files Content Replace】文件夹文件内容批量替换自动化测试脚本
c语言·python·pytest·adi
咖啡の猫1 小时前
Python字典的查询操作
数据库·python·c#
一只会奔跑的小橙子1 小时前
APP升级测试检查清单
自动化
smile_Iris1 小时前
Day 38 GPU训练及类的call方法
开发语言·python
嗷嗷哦润橘_2 小时前
AI Agent学习:MetaGPT项目之RAG
人工智能·python·学习·算法·deepseek
Smart-Space2 小时前
tkinter绘制组件(47)——导航边栏
python·tkinter·tinui