---
# System prepended metadata

title: 'T135315: Get UPS Tracking Shipment Details'
tags: [Wish]

---

###### tags: `Wish`

# T135315: Get UPS Tracking Shipment Details
*Joanna Jiang
2019-07-15*

---
## Summary
In UPS tracking page, 'Shipment Details' will be  available once a tracking id is valid, regardless of its transit or delivery status. Shown in Figure 1.

*<div style="text-align: center" > Figure 1 </div>*
![](https://i.imgur.com/rtTtKSr.png)


The input file is ['ups_tracking_number_0715.csv'](https://docs.google.com/spreadsheets/d/1lxzYoK_5R9uZapRpCkZ21UmJ2UrFxUFzCtKnJnzn_Ek/edit#gid=1566663646) containing one column of 100 UPS tracking number as in figure 2. And the output file is ['ups_shipping_details_0715.csv'](https://docs.google.com/spreadsheets/d/1yIdBS1n5y-nyak9margt5YXoznlxQsl-bVLUQAwvAoY/edit#gid=261584133) with UPS tracking number and 6 fact items associated as in figure 3.
<!-- ::: -->

*<div style="text-align: center" > Figure 2</div>*
![](https://i.imgur.com/cUWyVrh.png)




*<div style="text-align: center" > Figure 3 </div>*
![](https://i.imgur.com/3GGEk9d.png)


## Python script
```python=
import csv
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

TRACKING_ID_PATH = 'ups_tracking_number_0715.csv'
SHIP_DETAILS_PATH = 'ups_shipping_details_0715.csv'
UPS_TRACK_TEMPLATE = 'https://www.ups.com/track?loc=en_US&tracknum={}&requester=WT/trackdetails'
FIELDS = ['Tracking Number', 'Service', 'Weight', 'Shipped / Billed On', 'Shipment Category', 'Shipment Weight', 'Package Sequence Number']

# get tracking ids from csv file
track_id_file = open(TRACKING_ID_PATH)
track_id_reader = csv.reader(track_id_file)
track_ids = [row[0] for row in track_id_reader][1:]

# open shipping details file for writing (create if not exist)
ship_details_file = open(SHIP_DETAILS_PATH, mode='w')
ship_details_writer = csv.DictWriter(ship_details_file, fieldnames=FIELDS)
ship_details_writer.writeheader()

driver = webdriver.Chrome()

try:
    counter = 1

    for track_id in track_ids:
        print('Retrieving shipping details for package {}: {}'.format(counter, track_id))

        detail_dict = dict.fromkeys(FIELDS, '')
        detail_dict['Tracking Number'] = track_id

        # open the web page for a particular tracking id
        driver.get(UPS_TRACK_TEMPLATE.format(track_id))

        try:
            # locate and click on "Show More" button
            show_more_button = WebDriverWait(driver, 20)\
                .until(EC.element_to_be_clickable(
                    (By.CSS_SELECTOR, 'button#stApp_btnAdditionalInfoShowMore')))
            driver.execute_script("arguments[0].click();", show_more_button)

            WebDriverWait(driver, 20)\
                .until(EC.text_to_be_present_in_element(
                    (By.CSS_SELECTOR, 'button#stApp_btnAdditionalInfoShowMore'), "Show Less"))

            details_box = driver.find_element_by_css_selector('div.ups-list_box_content')

            details_list = details_box.find_elements_by_css_selector('div.ups-group.ng-tns-c11-2.ng-star-inserted')
            for detail in details_list:
                ps = detail.find_elements_by_tag_name('p')

                # get detail items key
                key = ps[0].text
                # get detail items value
                value = ps[1].text
                value = str.split(str(value), "\n")[0]

                if key in detail_dict:
                    detail_dict[key] = value
                else:
                    print("WARNING: unknown fact item", track_id, key, value)

            # if retrieving values doesn't succeed, only its tracking number is recorded
        except:
            pass

        # write all fact items
        print(detail_dict)
        ship_details_writer.writerow(detail_dict)
        ship_details_file.flush()

        counter += 1

finally:
    driver.quit()

```