python selenium关闭程序,没有回溯消息

2024-10-01 02:23:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python(2.7.7)脚本,我自动登录到一个网站,然后在浏览器自动关闭后结束程序。代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import sys

driver = webdriver.Ie()
driver.maximize_window()
driver.get("www.url.com")

# Find username field and input the correct username
inputElement = driver.find_element_by_id("...")
inputElement.send_keys('...')


# Find the password field and input the correct password
inputElement = driver.find_element_by_id("...")
inputElement.send_keys('...')

# Simulate pushing the ENTER key
inputElement.send_keys(Keys.ENTER)

i = 0
while i < 3600:
    driver.title
    time.sleep(1)
    i += 1
b = browser.find_by_tag("body")

这一切都很好,除了关闭浏览器和程序结束后,我会收到一条在python终端中显示的回溯消息:

回溯(最近一次呼叫): 文件“路径…”,第32行,输入 司机职务 文件“路径…”,第194行,标题中 响应=自我执行(Command.GET_标题) 文件“path…”,第173行,在execute中 self.error_处理程序.检查_响应(response) 文件“路径…”,第164行,在check_response中 引发异常_类(消息、屏幕、stacktrace) NoSuchWindowException:消息:u“无法获取浏览器”

我明白这是为什么,但这是故意的行为。我希望Python终端在浏览器关闭后立即关闭,没有消息。有什么想法吗?在


Tags: 文件thefromimport路径程序send消息
1条回答
网友
1楼 · 发布于 2024-10-01 02:23:44

最后,把剧本的结尾改成这样完成了我要做的事情:

try:
    i = 0
    while i < 10:
        driver.title
        time.sleep(1)
        i += 1
    b = browser.find_by_tag("body")

except:
    driver.close()
    subprocess.Popen("taskkill /f /IM IEDriverServer.exe")

当然,我还必须导入子进程库。在

相关问题 更多 >