我是靠谱客的博主 开放中心,这篇文章主要介绍selenium、webdriver打开Chrome浏览器闪退问题(版本号一致),现在分享给大家,希望可以做个参考。

使用selenium、webdriver打开谷歌浏览器,登录页面后闪退,但是版本号是对应的,是因为driver的全局变量问题

1、不设置driver为全局,放在函数内(会闪退)

from selenium import webdriver

# 登陆百度
def main():
    chromedriver_path = r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe"
    driver = webdriver.Chrome(executable_path=chromedriver_path)
    # 打开页面
    page = driver.get('https://www.baidu.com/')

if __name__ == "__main__":
    main()

2、把driver放在函数外,为全局(不会闪退)

from selenium import webdriver

chromedriver_path = r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver_path)
# 登陆百度
def main():
    # 打开页面
    page = driver.get('https://www.baidu.com/')

if __name__ == "__main__":
    main()

3、也可以把driver放在函数内,只要设置为全局变量就可以

from selenium import webdriver

# 登陆百度
def main():
    global driver
    chromedriver_path = r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe"
    driver = webdriver.Chrome(executable_path=chromedriver_path)
    # 打开页面
    page = driver.get('https://www.baidu.com/')

if __name__ == "__main__":
    main()

最后

以上就是开放中心最近收集整理的关于selenium、webdriver打开Chrome浏览器闪退问题(版本号一致)的全部内容,更多相关selenium、webdriver打开Chrome浏览器闪退问题(版本号一致)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(161)

评论列表共有 0 条评论

立即
投稿
返回
顶部