1

I have a requirement of automating the mail sending process with the data from DHL. Currently what we are doing is: We have a DHL account, someone has to manually login to the account , download the CSV dump which contains the order tracking details then upload it to the server, port the data from those and process it.

So I thought of automating the whole process so that it requires minimal manual intervention.

1) Is there anyway we can automate the download process from DHL?

Note: I'm using Python

Sandeep_Rao
  • 101
  • 1
  • 10
Sandeep
  • 671
  • 2
  • 7
  • 30

3 Answers3

2

I'd start by looking for something more convenient to access with code...

searching google for "dhl order tracking api" gives:

https://developer.dhl/api-catalog

as its first result, which looks useful and exposes quite a bit of functionality.

you then need to figure out how to make a "RESTful" request, which has answers here like Making a request to a RESTful API using python, and there are lots of tutorials on the internet if you search for things like "python tutorial rest client" which points to articles like this

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
  • Thanks a lot for the detailed response,i'll check on this and will update the thread soon. – Sandeep Jul 22 '19 at 11:32
  • the reason I suggested this (and why APIs like this are popular) is that user facing websites tend to regularly change in ways that tends to break code that interacts with them. developer centric APIs tend to be much more stable and friendly towards code. i.e. going with something like selenium (a great project I've used many times) will tend to need maybe 10 times as much code as just hitting a REST endpoint – Sam Mason Jul 22 '19 at 12:04
1

You can use Selenium for Python. Selenium is a package that automates a browser session. you can simulate mouse clicks and other actions using Selenium. To Install:

 pip install selenium

You will also have to install the webdriver for the browser you prefer to use. https://www.seleniumhq.org/projects/webdriver/

Make sure that the browser version that you are using is up to date.

Selenium Documentation: https://selenium-python.readthedocs.io/

Since you are dealing with passwords and sensitive data, I am not including the code.

Sandeep_Rao
  • 101
  • 1
  • 10
  • How selenium can help? I'm using python and after downloading the like i have to process the file and use that data for interim reporting purposes. So this might not help i think. – Sandeep Jul 22 '19 at 11:37
  • 1
    As per your requirement here's the flow: 1)Selenium opens DHL Login page URL 2) Sends Fields such as Password and Username automatically 3) Finds the csv download button using xPath 4) Clicks on the download button 5) Your program picks up the csv file using os module 5)Processing is performed 6) Send result email after processing using Python module SMTPLib. Run this entire process as frequently as required using Cronjob. Hope this helps, Thanks. – Sandeep_Rao Jul 22 '19 at 11:47
1
  • Login and Download

You can automate download process using selenium. Below is the sample code to automate any login process and download items from a webpage. As the requirements are not specific I'm taking general use-case and explaining how to automate the login and download process using python.

# Libraries - selenium for scraping and time for delay
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "Path to the directory to store downloaded files"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = r"Path to the directory where chrome driver is stored"
browser = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)

# To maximize the browser window 
browser.maximize_window() 

# web link for login page
browser.get('login page link') 

time.sleep(3) # wait for the page to load

# Enter your user name and password here. 
username = "YOUR USER NAME"
password = "YOUR PASSWORD"

# username send 
# you can find xpath to the element in developer option of the chrome
# referance answer "[https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome][1]"
a = browser.find_element_by_xpath("xpath to username text box") # find the xpath for username text box and replace inside the quotes
a.send_keys(username) # pass your username

# password send 
b = browser.find_element_by_xpath("xpath to password text box") # find the xpath for password text box and replace inside the quotes
b.send_keys(password) # pass your password

# submit button clicked 
browser.find_element_by_xpath("xpath to submit button").click() # find the xpath for submit or login button and replace inside the quotes

time.sleep(2) # wait for login to complete

print('Login Successful') # if there is no error you will see "Login Successful" message
# Navigate to the menu or any section using it's xpath and you can click using click() function
browser.find_element_by_xpath("x-path of the section/menu").click()

time.sleep(1)

# download file
browser.find_element_by_xpath("xpath of the download file button").click()
time.sleep(1)

# close browser window after successful completion of the process.
browser.close()  

This way you can automate the login and the downloading process.

  • Mail automation

For Mail automation use smtplib module, explore this documentation "https://docs.python.org/3/library/smtplib.html"

  • Process automation (Scheduling)

To automate the whole process on an everyday basis create a cron job for both tasks. Please refer python-crontab module. Documentation: https://pypi.org/project/python-crontab/enter link description here

By using selenium, smtplib, and python-crontab you can automate your complete process with minimal or no manual intervention.

Nitz
  • 81
  • 1
  • 4
  • 1
    how does this code determine if the login was successful? I'd expect there to be code checking the browser URL, maybe waiting for DOM elements to appear, or something other than a blind pause. so many other issues with this code... – Sam Mason Jul 22 '19 at 12:13
  • 1
    This is just to point him in the right direction, he can modify the code as per his requirements. Can use try-catch to check if the element is available. – Nitz Jul 22 '19 at 12:29