The scripts below read an excel sheet and find order numbers on the page; when one is found the script will enter the corresponding tracking number, return to the first page (which is the same page it originally searched) and continue looking through the order numbers.
Can anyone tell me why the following scripts slow down considerably when returning to that page? For instance: The scripts will work extremely well and searches about 5 order numbers a second and enters the tracking number and submits it very quick.
When the script goes back to the pages with the unshipped orders it takes VERY long to search for more of them (maybe 1 every MINUTE)
The "Not ups Tracking" and "Fedex" else functions does NOT get slow after completion (These are manually entered then the script is "restarted manually" by pressing enter)
The script looks like this:
list_of_files = glob.glob('F:/TrackingBot/GC/updater/*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
wb = xlrd.open_workbook(latest_file)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
for i in range(sheet.nrows):
cell = sheet.cell(i, 3)
cty = cell.ctype
if cty == xlrd.XL_CELL_EMPTY:
continue
else:
po = (sheet.cell_value(i, 3))
tracking = (sheet.cell_value(i, 10))
wayfair = "CS"
wayfsubstring = wayfair in po
if wayfsubstring == True:
continue
print("SEARCHING FOR: ", po)
if driver.find_elements_by_link_text(po):
print("FOUND!!!!", po, tracking)
driver.find_element_by_link_text(po).click()
ups = "1Z"
isSubstring = ups in tracking
if isSubstring == True:
cprint('UPS TRACKING NUMBER', 'green')
driver.implicitly_wait(25)
confirm = driver.find_element_by_link_text("""Confirm shipment""")
confirm.click()
time.sleep(1)
trackingnum = driver.find_element_by_xpath("""//input[contains(@data-test-id,
'text-input-tracking-id')]""")
trackingnum.click()
trackingnum.send_keys(tracking)
driver.find_element_by_xpath("""(//input[@value='Confirm shipment'])[2]""").click()
time.sleep(3)
driver.refresh()
time.sleep(4)
continue
else:
cprint("NOT UPS TRACKING", "red")
period = "."
isSubstring2 = period in tracking
if isSubstring2 == True:
cprint('NJ SENT TRACKING NUMBER', 'yellow')
input("Type to GO")
driver.refresh()
time.sleep(4)
continue
else:
cprint('FEDEX FREIGHT TRACKING NUMBER', 'green')
input("Type to GO")
driver.refresh()
time.sleep(4)
continue
The code above is for Amazon
Any advice is appreciated!
I finally figured out the problem, it was due to an implicitly wait.
The code snippet below shows where I initially set the wait.
if isSubstring == True:
cprint('UPS TRACKING NUMBER', 'green')
driver.implicitly_wait(25)
confirm = driver.find_element_by_link_text("""Confirm shipment""")
confirm.click()
The problem with that is the driver kept that wait time when going back through the loop so when the thread got back to:
print("SEARCHING FOR: ", po)
if driver.find_elements_by_link_text(po):
print("FOUND!!!!", po, tracking)
It waited for the max 25 second time before moving onto the next order number in the excel sheet. I fixed this by recalling another implicitly wait right before the loop searched for another order number. This way the wait needed later in the loop doesn't affect the searching as seen below:
print("SEARCHING FOR: ", po)
driver.implicitly_wait(.5)
if driver.find_elements_by_link_text(po):
print("FOUND!!!!", po, tracking)