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']")
I was able to login to gmail.I was able to compose a mail but I am not able to attach any file.Can anyone suggest me a way to attach file?Is that possible with selenium or do I have to use pyautoit module?
You're going a very difficult road here by trying to avoid the "right" way of doing things. Drop the current approach with selenium, and dive into the cold water, it's not that difficult.
This is a working example of sending an email with an attachment, once you understood MIME
you can do whatever you want with mails.
# -*- 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())