string formatting for Fedex bulk search

Viewed 25

I'm writing a webscrabing program and need to bulk search on FedEx, to do this I normally concatenate all my tracking numbers with "\n" between them to stand as an equivalent as pasting text from an excel column

issue is when I enter my string into their search box, it enters the number concatenated as if without the delimiter, so the search box only sees 1 long tracking number rather than multiple (lie if pasted from excel) any idea on how I can get the string formatted, or sent to the search box correctly?

this is what it looks like when I paste 2 tracking numbers 12345 and abcdefg:

wrong format

and here's what it should look like:

correct format

here is my code for sending the string to the search box:

def fedex_bulk(tn_list):
    # you can mostly ignore until the end of this function, all this is setup for the driver #
    # all relevant formatting is in the creating of variable search_str #
    driver = start_uc()
    loaded = False
    size=3
    tn_list  = [tn_list[i:i+size] if i+size <= len(tn_list) else tn_list[i:len(tn_list)] for i in range(0,len(tn_list),size)]
    tn_dict = []

    for sublist in tn_list:
        
        tries = 0

        ### concatenate all tracking numbers with "\n" delimiter
        search_str = ''
        for tn in sublist:
            search_str+=tn+'\n'

        ### loop until numbers searched or tried 4 times
        while not loaded:
            try:
                if tries==4:
                    break
                tries+=1

                ### refresh until loaded
                driver.get("https://www.fedex.com/en-us/tracking.html")
                page_loaded = False

                while not page_loaded:
                    try:
                        inputform = driver.find_element(By.XPATH, "//input[@class='form-input__element ng-pristine ng-invalid ng-touched']")
                        page_loaded = True
                    except: 
                        driver.refresh()
                        sleep(5)
                ### search_str sent to search box, formatted incorrectly
                inputform.send_keys(search_str)
                sleep(1)
                driver.find_element(By.XPATH, "//button[@type = 'submit']").click()

thankyou in advance!

1 Answers

I think the problem here is as following:
Inside the for sublist in tn_list: loop you do adding a '\n' to each tracking number tn in the sublist so search_str is containing a list of tracking numbers concatenated with '\n' between them.
But inside the while not page_loaded: you are locating the first input field and then you are sending to it all that long string containing multiple tracking numbers.
The search input element on the page is probably limited to accept valid inputs only, so it just ignores all the '\n' signs.
On the other hand, you are not inserting your tracking numbers to other search field inputs as you presenting on the picture showing how it should look.
So, in order to make your code work as you want you will probably need to insert single tracking number each time or to insert them to different search input fields.

Related