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 小时前
【武汉举办 | JPCS出版 | EI稳定检索】2026年智能制造、自动化与控制国际学术会议(IMAC 2026)
自动化·智能制造·控制·学术会议·会议推荐
niucloud-admin1 小时前
JAVA V6 多商户商城 开发文档——job 计划任务开发
java·python·github
小叶肥辉1 小时前
LangChain链和LangGraph图的学习笔记【三】——分别用langchain_openai库和langchain_community库调用大模型
笔记·python·langchain
2401_873479402 小时前
IP属地为什么有时显示外省?用IP查询工具核查动态分配、运营商出口与GeoIP库
python·tcp/ip·ip
海宇AI2 小时前
零信任架构实战:基于海宇柠檬查出险-登记证构建自动化车抵贷核保网关
java·人工智能·架构·自动化
OKkankan4 小时前
LangChain 能力详解!:输出解析、RAG、向量数据库与 Retriever 检索器
数据结构·python·langchain·ai应用
硬禾科技4 小时前
连接世界的工程哲学
嵌入式硬件·测试工具·自动化·硬件工程
YFJ_mily5 小时前
9.24早鸟通道即将关闭|IMRA2026智能制造机器人自动化IEEE会议|往届EI稳定检索,设有学生专属投稿权益
人工智能·机器学习·机器人·自动化·智能制造·rdlink研发家·马鞍山会议
山哥ol5 小时前
【Geany 环境配置与中文乱码解决参考】
python
会飞锦鲤5 小时前
基于 Mask R-CNN 的药片缺陷检测系统
人工智能·pytorch·python·神经网络·resnet-50