【发布时间】:2018-01-15 00:09:37
【问题描述】:
我正在尝试设置一个 python 脚本来测试服务器是否启动,如果没有,请给我发电子邮件。我目前正在尝试仅实现该目标的电子邮件部分。
我的代码如下:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
import smtplib
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path='C:\geckodriver-v0.18.0-win64\geckodriver.exe')
class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
def testServer(self):
driver = self.driver
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("email@gmail.com", "password")
#Send the mail
msg = "Testing if server is down" # The /n separates the message from the headers
server.sendmail("email@google.com", "email@google.com", msg)
driver.close()
if __name__ == "__main__":
unittest.main()
当我运行它时,我得到 driver.close() 语法不正确的错误。 但是,它适用于我拥有的所有其他测试脚本。 在出现此错误之前,脚本只会运行并且永远不会关闭,并且永远不会发送电子邮件。我不确定我错过了什么。如果您看到问题,请告诉我。谢谢!
【问题讨论】: