简体   繁体   中英

Edit Python Gui

I have a piece of code to aid me in sending emails to clients. This piece of code is very basic and is written with Python3 and utilizes Selenium + Chromedriver to send the emails on my behalf. (I am not a programmer please be patient with messy code)

The problem is when I need to reach mass amounts of email addresses. Every time I need to manually edit the subject/bcc/cc fields in the code through Sublime text to ensure the right client is reached and this takes a minute or 2.

It is also important that each client is emailed separately as I work with a CRM and want it to be bcc'd into every email to keep track of what is happening in the company. This means all the clients cannot be cc'd into one specific email.

So if I need to send 500 emails, it takes quite some time to edit the code 500 times with the correct email addresses.

is there any way to simplify this editing of code? For example: Have a window open that asks me what email addresses I want to send to > insert email addresses > program.py code is edited accordingly > runs code > emails all specified clients

I have not tried much because honestly I have no idea where to even begin looking for a solution, neither do I know if something like this is even possible.

import selenium
from selenium import webdriver
from time import sleep
import time

driver = webdriver.Chrome()
driver.get('WEBSITE URL')
time.sleep(5)

emailelement = driver.find_element_by_id("identifierId")
time.sleep(3)
emailelement.send_keys('XXXXXXXX')
time.sleep(1)
Logginelement = driver.find_element_by_xpath("//span[@class='RveJvd snByac']")
Logginelement.click()
time.sleep(3)
passelement = driver.find_element_by_name('password')
passelement.send_keys('XXXXXXXX')
time.sleep(1)
Logginelement = driver.find_element_by_xpath("//span[@class='RveJvd snByac']")
Logginelement.click()
time.sleep(3)

Compose = driver.find_element_by_xpath("//div[@class='T-I J-J5-Ji T-I-KE L3']")
Compose.click()
time.sleep(1)

BCC = driver.find_element_by_xpath("//span[@class='aB  gQ pB']")
BCC.click()
time.sleep(0.5)

bcc = driver.find_element_by_xpath("//textarea[@name='bcc']")
bcc.send_keys('EMAIL ADDRESS')
time.sleep(0.5)

Receipient = driver.find_element_by_xpath("//textarea[@name='to']")
Receipient.send_keys('EMAIL ADDRESS')
time.sleep(0.5)

Subject = driver.find_element_by_xpath("//input[@name='subjectbox']")
Subject.send_keys('SUBJECT')
time.sleep(0.5)

Message = driver.find_element_by_xpath("//div[@class='Am Al editable LW-avf tS-tW']")
Message.send_keys('MESSAGE')
time.sleep(0.5)

#Attach not utilized, just for experimentation.
#Attach = driver.find_element_by_xpath("//div[@class='a1 aaA aMZ']")
#Attach.click()
#time.sleep(15)

Send = driver.find_element_by_xpath("//div[@class='T-I J-J5-Ji aoO v7 T-I-atl L3']")
Send.click()
time.sleep(3)

My question is not specifically pertaining to the code. I am wondering if it would be possible to edit certain lines in the code using a Gui. For example: "bcc.send_keys('EMAIL ADDRESS')" Editing the EMAIL ADDRESS by using an external window with something like Tkinter?

Please any advice or pointers to the right library/documentation would be greatly appreciated! :)

So what you are asking for is called a for loop. This is used to perform a task multiple times over and typically used with something like a list.

Three other things I have noticed:

  1. You are using a lot of sleep() calls and looking at your code I do not think this is needed.

  2. You are never calling the variables you have assigned so to reduce your code I got rid of all the variable calls.

  3. I think you are misunderstanding how imports work do to how you import sleep and time. import time works fine for your time.sleep() call without the need to do from time import sleep . If you just want to use the sleep() method from time then do from time import sleep and simply call sleep() in your code. You will not need the time. prefix in that case.

I have edited you code to include a for loop and an example list you can put your own emails into. As well as a variable for the BCC email.

Take a look at this example and let me know what you think:

import selenium
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('WEBSITE URL')

# use a list object to store all your emails.
emails = ['someone@someplace.com', 'someone_else@someplace.com']
bcc_email = 'email_to_bcc@someplace.com'

# use a for loop to send all your emails.
for email in emails:
    driver.find_element_by_id("identifierId").send_keys('XXXXXXXX')
    driver.find_element_by_xpath("//span[@class='RveJvd snByac']").click()
    driver.find_element_by_name('password').send_keys('XXXXXXXX')
    driver.find_element_by_xpath("//span[@class='RveJvd snByac']").click()
    driver.find_element_by_xpath("//div[@class='T-I J-J5-Ji T-I-KE L3']").click()
    driver.find_element_by_xpath("//span[@class='aB  gQ pB']").click()
    # notice the variable in the email section
    driver.find_element_by_xpath("//textarea[@name='bcc']").send_keys(bcc_email)
    driver.find_element_by_xpath("//textarea[@name='to']").send_keys(email)
    driver.find_element_by_xpath("//input[@name='subjectbox']").send_keys('SUBJECT')
    driver.find_element_by_xpath("//div[@class='Am Al editable LW-avf tS-tW']").send_keys('MESSAGE')
    driver.find_element_by_xpath("//div[@class='T-I J-J5-Ji aoO v7 T-I-atl L3']").click()

For a simple GUI option that allows you to send using any email you want to provide take a look at this example:

Note this example does not have any error checking so you may want to add in some kind of email validation first.

import tkinter as tk
import selenium
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('WEBSITE URL')
bcc_email = 'email_to_bcc@someplace.com'


root = tk.Tk()

email = tk.Entry(root)
lbl1 = tk.Label(root, text='Enter single email here: ')
lbl1.grid(row=0, column=0)
email.grid(row=0, column=1)


def process_emails():
    # use a for loop to send all your emails.
    driver.find_element_by_id("identifierId").send_keys('XXXXXXXX')
    driver.find_element_by_xpath("//span[@class='RveJvd snByac']").click()
    driver.find_element_by_name('password').send_keys('XXXXXXXX')
    driver.find_element_by_xpath("//span[@class='RveJvd snByac']").click()
    driver.find_element_by_xpath("//div[@class='T-I J-J5-Ji T-I-KE L3']").click()
    driver.find_element_by_xpath("//span[@class='aB  gQ pB']").click()
    # notice the variable in the email section
    driver.find_element_by_xpath("//textarea[@name='bcc']").send_keys(bcc_email)
    driver.find_element_by_xpath("//textarea[@name='to']").send_keys(email.get())
    driver.find_element_by_xpath("//input[@name='subjectbox']").send_keys('SUBJECT')
    driver.find_element_by_xpath("//div[@class='Am Al editable LW-avf tS-tW']").send_keys('MESSAGE')
    driver.find_element_by_xpath("//div[@class='T-I J-J5-Ji aoO v7 T-I-atl L3']").click()


tk.Button(root, text='Process single email!', command=process_emails).grid(row=1, column=0, columnspan=2)
root.mainloop()

Here is a simple GUI app that you could add to your code to get a window that ask for a list of emails being separated with "; ". You could then use the suggested above for loop to process the list of emails. You would really need to add error checking as well in case you have bad data or an email failure.

from tkinter import *

def get_emails():
    email_str = my_stringvar.get()
    email_list.append(email_str.split("; "))

email_list = []
my_window = Tk()
my_stringvar = StringVar()
my_entry = Entry(textvariable=my_stringvar)
my_button = Button(text="Done", command=get_emails)

If you want to do one at a time you can just take out the split.

from tkinter import *

def get_emails():
    email_str = my_stringvar.get()

email_str = ""
my_window = Tk()
my_stringvar = StringVar()
my_entry = Entry(textvariable=my_stringvar)
my_button = Button(text="Done", command=get_emails)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM