selenium - 使用 Selenium Webdriver 启动后,Google chrome 会自动关闭
问题描述
我在 Windows 10 上使用 Selenium 和 Python 3.7.3。
如果我将代码包装在一个类中,浏览器会在打开页面后立即终止:
'''
Program to show how to open chrome browser using selenium webdriver
'''
from selenium import webdriver
#import os
class run_chrome_tests(object):
def test_method(self):
# This is the location of the chrome driver saved into a variable
#driver_location = "D:\\Udemy_Python\\Libs\\chromedriver.exe"
# Letting the system environment know the location of the chrome driver
#os.environ["webdriver.chrome.driver"] = driver_location
# Letting the chrome browser know the location of the chrome driver
driver = webdriver.Chrome()
driver.get("http://www.letskodeit.com")
ch = run_chrome_tests()
ch.test_method()
解决方案
检查 Chrome 浏览器的版本和 chrome 驱动程序的版本,如果驱动程序不兼容,则浏览器在打开后立即终止,请尝试同时使用最新版本
推荐阅读
- php - 按 ID 查询帖子总是返回最新的帖子(WordPress)
- swift - 使用已经存在的覆盖函数 layerClass 向 UIView 类添加阴影
- sql-server - 限制 SQL Server 事务日志
- github - 在存储库中未检测到 GitHub Actions 工作流
- python - Python程序子类化一个PyQt5窗口不能在函数中设置window.title
- graph - 如何使用 Microsoft Graph 提供完整的邮箱访问权限?
- c# - 在文本文件中记录 Mongo 驱动程序查询性能
- java - ApplicationContext.getBeansOfType 改变返回的映射 - WebFluxTest SpringBoot
- docker - 每次我 ssh 到新机器时,docker 可以帮助我避免重新安装库的过程吗?
- xamarin - 是否可以使用 Xamarin.Essetianls.Browser 打开嵌入的 HTML 文件