阅读 171

selenium、webdriver打开Chrome浏览器闪退问题(版本号也是一致时)

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

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

 

from selenium import webdriver

# 登陆百度
def main():
    chromedriver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.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)\Google\Chrome\Application\chromedriver.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)\Google\Chrome\Application\chromedriver.exe"
    driver = webdriver.Chrome(executable_path=chromedriver_path)
    # 打开页面
    page = driver.get(https://www.baidu.com/)

if __name__ == "__main__":
    main()

非原创,自用记录

原文:https://www.cnblogs.com/Tgbshare/p/14817222.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐