2017-08-07 81 views
1

我想設置一個測試服務器是否啓動的python腳本,如果不是,請給我發電子郵件。我目前正試圖實現該目標的電子郵件部分。設置一個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 protected]", "password") 

     #Send the mail 
     msg = "Testing if server is down" # The /n separates the message from the headers 
     server.sendmail("[email protected]", "[email protected]", msg) 
     driver.close() 

if __name__ == "__main__": 
    unittest.main() 

當我運行它,我得到driver.close(錯誤)是語法不正確。 但是,它對我所有其他測試腳本都正常工作。 在彈出此錯誤之前,腳本將運行並永不關閉,並且電子郵件永遠不會被髮送。我不確定我錯過了什麼。請讓我知道,如果你看到這個問題。謝謝!

回答

1

你必須開括號:

server.sendmail(("[email protected]", "[email protected]", msg) 

更改爲:

server.sendmail("[email protected]", "[email protected]", msg) 
+0

我固定的問題,但我仍然得到關於driver.close() –

+0

你仍然得到同樣的問題一個無效的語法錯誤或者是你的代碼拋出異常?什麼是錯誤信息? – LeopoldVonBuschLight

+0

每一個說法都沒有錯誤。代碼「運行」,或者更確切地說它出現在終端中,除非它永不停止,並且電子郵件永遠不會被髮送。這就像腳本打開,但沒有做任何事情或關閉 –