使用python写段脚本代码,访问URL https://elias.szlanyou.com/#.default,输入账号和密码,点击进入主页后,将网页页面截图,将截图放入到邮件正文或附件,将邮件发出给某人
时间: 2024-04-05 12:32:18 浏览: 72
以下是Python脚本示例:
```python
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
# 设置Chrome浏览器参数
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 设置账号和密码
username = 'your_username'
password = 'your_password'
# 设置收件人和发件人
to_email = 'recipient@example.com'
from_email = 'sender@example.com'
password = 'your_password'
# 打开Chrome浏览器
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
# 访问网站并输入账号密码
driver.get('https://elias.szlanyou.com/#.default')
time.sleep(2)
driver.find_element_by_css_selector('#username').send_keys(username)
driver.find_element_by_css_selector('#password').send_keys(password)
driver.find_element_by_css_selector('#login').click()
# 等待页面加载完成
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#home-page')))
# 截图并将其保存为图片文件
screenshot_file = 'screenshot.png'
driver.get_screenshot_as_file(screenshot_file)
# 将图片添加到邮件中
msg = MIMEMultipart()
image = MIMEImage(open(screenshot_file, 'rb').read())
msg.attach(image)
# 发送邮件
msg['Subject'] = '网页截图'
msg['From'] = from_email
msg['To'] = to_email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email, password)
server.sendmail(from_email, to_email, msg.as_string())
server.quit()
# 关闭浏览器
driver.quit()
```
在脚本中,我们使用Selenium来控制Chrome浏览器,并访问网站https://elias.szlanyou.com/#.default。然后,我们输入账号和密码,并点击登录按钮。等待页面加载完成后,我们使用`get_screenshot_as_file`方法来截图并将其保存为图片文件。最后,我们将图片添加到邮件中,并使用SMTP协议发送邮件。发送邮件的部分需要您根据您的实际情况进行修改。
阅读全文