【发布时间】:2019-06-13 08:44:03
【问题描述】:
我试图在 Pycharm IDE 中的 chrome 浏览器中运行一些 selenium 测试。我在函数中编写了 chrome 驱动程序,当我尝试运行代码时,它会打开浏览器并在一秒钟内自动关闭。但是当我写功能之外的chrome驱动程序它打开浏览器并且没有关闭。如果我在方法/函数中编写 chromedriver 代码,如何使浏览器保持打开状态?
代码:
from selenium import webdriver
import os
class Chrome:
def Run(self):
driverLocation="F:\\Workspace py\chromedriver\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = driverLocation
driver = webdriver.Chrome(driverLocation)
driver.get("https://www.google.com")
Test=Chrome()
Test.Run()
【问题讨论】:
-
由于
driver是Run函数中的一个局部变量,我敢打赌Python解释器会自动关闭浏览器窗口,因为Run函数退出了。
标签: python-3.x selenium