【发布时间】:2019-02-13 17:29:41
【问题描述】:
我正在使用 3rd 方软件创建一个带有多个按钮的精美应用程序 GUI。
每个按钮将执行不同的 .py 文件/.exe 文件。例如:-
btnYahoo = execute yahoo.py/yahoo.exe
btnGoogle = execute google.py/google.exe
所以在两个 py 脚本中都使用 chromedriver 来启动 Chrome 浏览器并重定向到特定的 URL
google.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://www.google.com")
yahoo.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://malaysia.yahoo.com/?p=us")
所以如果我执行上面的两个脚本,它将启动 2 个 chrome 浏览器。
因此,我关心的是如何检查 webdriver.Chrome 是否正在运行?
如果是这样,则将 webdriver.Chrome 分配给一个变量,以便我可以打开新选项卡并进一步自动化脚本进度。
预期结果示例:
执行 google.py - 新的 chrome 浏览器打开并重定向到 www.google.com
执行 yahoo.py - 如果 webdriver.Chrome 已执行/存在,则将浏览器分配给驱动程序变量。否则启动新浏览器
感谢您提前提供信息。
【问题讨论】:
-
你应该打电话给
driver.quit()。如果在程序结束时不使用quit(),WebDriver 会话将不会正确关闭,文件也不会从内存中清除。这可能会导致内存泄漏错误。这可以在任务管理器中查找:它将保留以前运行的 chromedriver.exe。这发生在 JavaScript 版本的 selenium-webdriver 客户端中,也可能是 Python 的情况。 -
@cnishina 我不想关闭网络驱动程序。相反,我只想检查 webdriver 是否存在。如果是,则将 webdriver 分配给一个变量,否则启动新的浏览器。
-
您不需要使用 Selenium 来启动带有 URL 的浏览器。检查如何为您使用的操作系统执行此操作。它应该像打开提供的 URL 一样简单...应该使用默认浏览器并导航到该 URL。
-
@JeffC 请正确阅读我的帖子。我曾提到“进一步自动化脚本进度”。这意味着我使用 Selenium 不仅仅是为了重定向。谢谢
标签: python selenium-webdriver selenium-chromedriver