class MSC: def __init__(self, bl_number): # Instantiates Driver class and opens url in headless browser self.driver = Driver() self.driver.get_page(MSCURL) self.driver.add_cookies({ 'name': 'newsletter-signup-cookie', 'value': 'temp-hidden' }) self.bl_number = bl_number # Locates search box and inputs BL number element = self.driver.find_id_element( "ctl00_ctl00_plcMain_plcMain_TrackSearch_txtBolSearch_TextField") element.send_keys('MEDUQ' + self.bl_number) # BL Format: 1312550 # Locates search button and clicks search_button_xpath = '//*[@id="ctl00_ctl00_plcMain_plcMain_TrackSearch_pnlTrackSearchForm"]/div/div[2]' search = self.driver.find_xpath_element(search_button_xpath) search.click() def get_loading_port(self): pol_xpath = '//*[@id="ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_pnlBOLContent"]/table/tbody[1]/tr/td[3]' load_port_element = self.driver.find_xpath_element(pol_xpath) loading_port = load_port_element.text.split(',') return loading_port[0] def get_departure_date(self): etd_xpath = '//*[@id="ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_pnlBOLContent"]/table/tbody[1]/tr/td[1]/span' etd_element = self.driver.find_xpath_element(etd_xpath) departure_date = etd_element.text.split('/') return departure_date[1] + departure_date[0] def get_discharge_port(self): pod_xpath = '//*[@id="ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_rptContainers_ctl01_pnlContainer"]' \ '/table[2]/tbody/tr[1]/td[1]/span' pod_element = self.driver.find_xpath_element(pod_xpath) discharge_port = pod_element.text.split(',')[:2] return ','.join(discharge_port) def get_arrival_date(self): global arrival_date, arrival_info eta_xpath = '//*[@id="ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_rptContainers_ctl01_pnlContainer"]/table[2]/tbody/tr' eta_elements = self.driver.find_xpath_elements(eta_xpath) for element in eta_elements: if 'Discharged' or 'Arrival' in element.text: arrival_info = element.text.split(' ') break for item in arrival_info: if '/' in item: arrival_date = item.split('/') return arrival_date[1] + arrival_date[0] def get_containers(self): # Locates containers (e.g. ABCD1234567) elements on page container_list = [] container_elements = self.driver.find_xpath_elements( '//*[@class="containerToggle"]') time.sleep(1) i = 1 for element in container_elements: element.click() raw_container = element.text.split(' ') container_number = raw_container[1] container_size_element = self.driver.find_xpath_element( '//*[@id="ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_' 'rptContainers_ctl0{}_pnlContainer"]/table[1]/tbody[1]' '/tr/td[1]/span'.format(i)) container_size = container_size_element.text[:2] container_list.append((container_number, container_size)) i += 1 cookie_popup = self.driver.find_xpath_element( '//*[@id="cookiePolicyModal"]/div/div/a') cookie_popup.click() container_elements[0].click() return container_list def get_all_info(self): containers = self.get_containers() load_port = self.get_loading_port() etd = self.get_departure_date() discharge_port = self.get_discharge_port() eta = self.get_arrival_date() return { 'MBL': self.bl_number, 'Info': { 'POL': load_port, 'ETD': etd, 'POD': discharge_port, 'ETA': eta, 'Containers': containers } }
class Cosco: def __init__(self, bl_number): self.driver = Driver() dummy_url = '/error404' self.bl_number = bl_number # BL format : 6194326880 self.driver.get_page(COSCOURL + dummy_url) self.driver.add_cookies() self.driver.get_page(COSCOURL + '/cargoTracking') # Locates search box and inputs BL number search_element = self.driver.find_xpath_element( '//*[@id="wrap"]/input') search_element.send_keys(bl_number) # Locates search button and clicks search_xpath = '/html/body/div[1]/div[4]/div[1]/div/div[1]/div/div[2]/form/div/div[2]/button' search_button = self.driver.find_xpath_element(search_xpath) search_button.click() # Locates table containing majority of information main_info_table = self.driver.find_class_element('ivu-c-detailPart') main_info_text = main_info_table.text raw_info = main_info_text.replace(' ', '') self.main_info = raw_info.split('\n') # Locates table containing secondary information secondary_xpath = '/html/body/div[1]/div[4]/div[1]/div/div[2]/div/div/div[2]/div[1]/div[2]/div' secondary_table = self.driver.find_xpath_element(secondary_xpath) secondary_info_text = secondary_table.text self.secondary_info = secondary_info_text.split('\n') def get_cosco_pol(self): raw_pol = self.main_info[9] load_port = raw_pol.split('-') return load_port[0] def get_cosco_etd(self): raw_etd = self.secondary_info[7] raw_etd = raw_etd.replace('-', ' ') raw_etd_split = raw_etd.split(' ') etd = raw_etd_split[1] + raw_etd_split[2] return etd def get_cosco_pod(self): raw_pod = self.main_info[11] discharge_port = raw_pod.split('-') return discharge_port[0] def get_cosco_eta(self): raw_eta = self.secondary_info[-5] raw_eta = raw_eta.replace('-', ' ') raw_eta_split = raw_eta.split(' ') eta = raw_eta_split[1] + raw_eta_split[2] return eta def get_cosco_containers(self): container_xpath = '/html/body/div[1]/div[4]/div[1]/div/div[2]' \ '/div/div/div[2]/div[1]/div[5]/div/div/div[2]/table/tbody/tr' container_table = self.driver.find_xpath_elements(container_xpath) container_list = [] for container in container_table: raw_container = container.text raw_container = raw_container.split('\n') container_number = raw_container[0] container_type = raw_container[1] container = (container_number, container_type) container_list.append(container) return container_list def get_cosco_telex(self): telex = self.main_info[23] if telex == 'SeaWayBill': telex_released = 'Yes' else: telex_released = 'No' return telex_released def get_all_cosco_info(self): port_of_discharge = self.get_cosco_pod() port_of_loading = self.get_cosco_pol() telex = self.get_cosco_telex() etd = self.get_cosco_etd() eta = self.get_cosco_eta() containers = self.get_cosco_containers() return { 'MBL': self.bl_number, 'Info': { 'POL': port_of_loading, 'ETD': etd, 'POD': port_of_discharge, 'ETA': eta, 'Containers': containers, 'TELEX': telex } }