2017-01-31 3 views
0
import os 
import sys 
import selenium 
import time 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

browser = webdriver.Firefox() 
type(browser) 
Get_webpage=browser.get('https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en&service=mail#identifier') 

user_name = browser.find_element_by_id('Email') 
user_name.send_keys("username")#Enter your username 

time.sleep(2) 

next = browser.find_element_by_id('next') 
next.submit() 

time.sleep(5) 

password = browser.find_element_by_id('Passwd') 
password.send_keys("password")#enter your password 
password.submit() 

time.sleep(5) 
compose = browser.find_element_by_xpath("//div[@role='button']") 
compose.click() 

time.sleep(5) 
Attach_file = browser.find_element_by_xpath("//div[@role='button']") 

Gmail에 로그인 할 수 있었지만 메일을 작성할 수 있었지만 파일을 첨부 할 수 없었습니다. 누구나 파일 첨부 방법을 제안 할 수 있습니까? 셀렌으로 가능합니까, 아니면해야합니까? pyautoit 모듈을 사용 하시겠습니까?pyautoit 모듈을 사용하여 파일을 Gmail에 첨부 할 수 있습니까?

+0

셀렌을 사용하려는 시도를 존중하지만, 왜 Google API를 사용하지 않습니까? https://developers.google.com/gmail/api/ – RobertB

+0

Google api를 사용할 수 있는지 알지 못했습니다. 우편물을 보내는 동안 첨부 파일을 어떻게 사용할 수 있는지 안내해 주시겠습니까? 제발 자바가 아닌 파이썬을 사용하여 자동화 할 필요가 없습니다. – Sai

+0

내가 게시 한 링크에는 가이드, 샘플 및 많은 설명서가 있습니다. 가이드에는 심지어 파이썬 예제가 있습니다. "첨부 파일 업로드하기"에 대한 안내서도 있습니다. 그래서 나는 당신이 읽고 배우는 것만으로 생각합니다. – RobertB

답변

1

"올바른"방식으로 작업하는 것을 피하려고하면 매우 어려운 길을 가고 있습니다. 현재의 접근법을 셀레늄으로 버리고 차가운 물속으로 뛰어 들어보세요. 그리 어렵지 않습니다.

이것은 첨부 파일이 포함 된 이메일을 보내는 작업 예제입니다. MIME을 사용하면 메일로 원하는 모든 작업을 수행 할 수 있습니다.

# -*- coding: iso-8859-1 -*- 
from email.mime.text import MIMEText 
from email.mime.application import MIMEApplication 
from email.mime.multipart import MIMEMultipart 
from smtplib import SMTP 

msg = MIMEMultipart() 
msg['Subject'] = 'Email From Python' 
msg['From'] = '[email protected]' 
msg['To'] = '[email protected]' 

# That is what u see if dont have an email reader: 
msg.preamble = 'Multipart massage.\n' 

# This is the textual part: 
part = MIMEText("Hello im sending an email with a PDF from a python program") 
msg.attach(part) 

# This is the binary part(The Attachment): 
part = MIMEApplication(open("networkanalyze.pdf","rb").read()) 
part.add_header('Content-Disposition', 'attachment', filename="file.pdf") 
msg.attach(part) 

# Create an instance in SMTP server 
smtp = SMTP("smtp.gmail.com:587") 
smtp.ehlo() 
smtp.starttls() 
smtp.login("[email protected]", "mySuperSecretPassword") 
smtp.close() 

# Send the email 
smtp.sendmail(msg['From'], msg['To'], msg.as_string()) 
+0

응답 해 주셔서 감사합니다. 첨부해야하는 파일의 경로는 어디에서 언급해야합니까? – Sai

+0

파일이 .py 파일과 동일한 디렉토리에 있으면 경로를 언급하지 않아도됩니다. 이 부분은 다음과 같이 편집하십시오 : 'part = MIMEApplication (open ("\ path \ to \ file \ networkanalyze.pdf", "rb")) –

+0

이 답변으로 도움이 되었다면, 대답을 수락합니다. –