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键退出...") 
相关推荐
52Hz1181 天前
二叉树理论、力扣94.二叉树的中序遍历、104.二叉树的最大深度、226.反转二叉树、101.对称二叉树
python·算法·leetcode
卖个几把萌1 天前
解决 Python 项目依赖冲突:使用 pip-tools 一键生成现代化的 requirements.txt
开发语言·python·pip
黎雁·泠崖1 天前
Java字符串入门:API入门+String类核心
java·开发语言·python
晚风予卿云月1 天前
【Linux】自动化构建—make/Makefile
linux·自动化·make/makefile
哈哈不让取名字1 天前
用Pygame开发你的第一个小游戏
jvm·数据库·python
程序员敲代码吗1 天前
Python异步编程入门:Asyncio库的使用
jvm·数据库·python
sunfove1 天前
Python小游戏:在 2048 游戏中实现基于线性插值(Lerp)的平滑动画
开发语言·python·游戏
2501_944526421 天前
Flutter for OpenHarmony 万能游戏库App实战 - 抽牌游戏实现
android·开发语言·python·flutter·游戏
副露のmagic1 天前
python基础复健
python·算法
学Linux的语莫1 天前
python项目打包为镜像
java·python·spring