本文的示例代码参考selenium-basic
目录
Python
python --version # Python 3.5.2
pip install Selenium
关于Python安装 可以参考pyenv
ChromeDriver
Google Chrome 69.0.3497.100 (正式版本) (64 位)
- 接着下载并安装对应版本ChromeDriver
sudo mv ~/Downloads/chromedriver /usr/bin
Selenium
vim demo.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from time import sleep
import unittest
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.login_url = 'https://ac.zhgcloud.com'
self.login_username = '18888888888'
self.login_password = '******'
self.console_url = 'https://console.zhgcloud.com/machines/all'
def testLogin(self):
self.driver.get(self.login_url)
sleep(3)
try:
self.driver.find_element_by_id('platform_unique_key').send_keys(
self.login_username)
self.driver.find_element_by_id('platform_password').send_keys(
self.login_password)
self.driver.find_element_by_id('platform_password').send_keys(
Keys.ENTER)
sleep(3)
self.assertTrue(self.driver.current_url == self.console_url)
except NoSuchElementException as ex:
print(ex)
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
- 测试
python demo.py
.
----------------------------------------------------------------------
Ran 1 test in 11.091s
OK
vim send_email.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def send_email(msg):
try:
server = smtplib.SMTP('smtp.qq.com', 25)
server.login('yl33643@foxmail.com', 'ivktvlzhmtticabh')
message = MIMEText(str(msg), 'plain', 'utf-8')
message['From'] = Header('yl33643@foxmail.com', 'utf-8')
message['To'] = Header('yuanlin@zeaho.com', 'utf-8')
message['Subject'] = Header('基于Selenium的Web自动化测试', 'utf-8')
server.sendmail('yl33643@foxmail.com', ['yuanlin@zeaho.com'],
message.as_string())
server.quit()
except smtplib.SMTPException as ex:
print(ex)
vim demo.py
// 省略了未修改的代码
import send_email
import unittest
class Login(unittest.TestCase):
// 省略了未修改的代码
def testLogin(self):
// 省略了未修改的代码
except NoSuchElementException as ex:
send_email.send_email(ex)
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
Crontab
sed -i "" 's/platform_unique_key/platform_unique_key1/g' demo.py
echo "* * * * * /Users/kevin/.pyenv/shims/python /Users/kevin/Workspace/web-tutorial/selenium-basic/demo.py" >> cron.txt
crontab cron.txt
crontab -l # crontab -r
关于contab规范 可以参考19. crontab 定时任务 & 在线工具 - Crontab
网友评论