def getorderinfo3(driver0, orderid): bMutex.lock() js = 'window.open(\"https://sellercentral.amazon.com/gp/orders-v2/details?orderID=' + orderid + '\");' handle = driver0.current_window_handle driver0.execute_script(js) handles = driver0.window_handles for newhandle in handles: # 筛选新打开的窗口B if newhandle != handle: # 切换到新打开的窗口B driver0.switch_to_window(newhandle) # 在新打开的窗口B中操作 wait.WebDriverWait(driver0, 10000000).until( EC.presence_of_element_located( (By.ID, 'myo-order-details-item-product-details'))) o1 = driver0.find_element_by_xpath( "//a[contains(@href,'https://www.amazon.com/gp/product/')]").text # 关闭当前窗口B driver0.close() # 切换回窗口A driver0.switch_to_window(handle) bMutex.unlock() return o1
def run(self): try: bMutex.lock() self.window.driver = webdriver.Firefox( executable_path=self.window.driverPath, firefox_profile=self.window.profile) self.window.setDriver(self.window.driver) bMutex.unlock() time.sleep(60) self.window.modelText = self.window.getModelInputWidget( ).toPlainText() self.window.nameList = [] allID = self.window.getIDInputWidget().toPlainText() allIDList = allID.split('\n') # 先把之前生成的发送情况给清除掉 t = open('allID.txt', 'w') t.close() t = open('sendID.txt', 'w') t.close() for i in allIDList: if i == '': continue f = open('allID.txt', 'a+') f.write(i + '\n') f.close() orderSizeStr = str(len(allIDList)) self.window.s.scheduleSignal.emit(str(0) + r'/' + orderSizeStr) completedIndex = 0 for i in allIDList: if i == '': continue sendUrl = send.generateSendMessageUrl(i) thisName = send.sendMessage(sendUrl, self.window.modelText, self.window.driver, i) self.window.nameList.append(thisName) haveSentID = open('sendID.txt', 'a+') haveSentID.write(i + '\n') haveSentID.close() completedIndex = completedIndex + 1 self.window.s.addLogItemSignal.emit('Order ID:' + str(i) + '\nUser Name' + thisName) self.window.s.scheduleSignal.emit( str(completedIndex) + r'/' + orderSizeStr) self.window.driver.close() self.window.s.informationSignal.emit('information', 'completed successfully') self.window.s.ungraySignal.emit() except Exception as e: bMutex.unlock() traceback.print_exc() self.window.s.errorSignal.emit()
def run(self): try: bMutex.lock() self.window.driver = webdriver.Firefox( executable_path=self.window.driverPath, firefox_profile=self.window.profile) self.window.setDriver(self.window.driver) self.window.modelText = self.window.getModelInputWidget( ).toPlainText() # 先把之前生成的发送情况给清除掉 t = open('allID.txt', 'w') t.close() t = open('sendID.txt', 'w') t.close() # 获取orderid,并创建各种list self.window.driver.get(self.window.selectDateUrl) bMutex.unlock() self.window.orderList, self.window.dateList = tool.getlist( self.window.driver) self.window.currentThreadSenderList = [] self.window.nameList = [] # 将allID写入 for i in self.window.orderList: allID = open('allID.txt', 'a+') allID.write(i + '\n') allID.close() orderSizeStr = str(len(self.window.orderList)) self.window.s.scheduleSignal.emit(str(0) + r'/' + orderSizeStr) completedIndex = 0 for i in self.window.orderList: sendUrl = send.generateSendMessageUrl(i) thisName = send.sendMessage(sendUrl, self.window.modelText, self.window.driver, i) self.window.nameList.append(thisName) haveSentID = open('sendID.txt', 'a+') haveSentID.write(i + '\n') haveSentID.close() completedIndex = completedIndex + 1 self.window.s.addLogItemSignal.emit('Order ID:' + str(i) + '\nUser Name' + thisName) self.window.s.scheduleSignal.emit( str(completedIndex) + r'/' + orderSizeStr) self.window.driver.close() self.window.s.informationSignal.emit('information', 'completed successfully') self.window.s.ungraySignal.emit() except Exception as e: bMutex.unlock() traceback.print_exc() self.window.s.errorSignal.emit()
def getcurrent(driver0, orderid): while True: try: time.sleep(2) bMutex.lock() wait.WebDriverWait(driver0, 5).until( EC.presence_of_element_located((By.ID, 'search-text-box'))) idinput = driver0.find_element_by_id("search-text-box") searchbutton = driver0.find_element_by_name("Search") driver0.execute_script("arguments[0].value=" + "'" + orderid + "'", idinput) driver0.execute_script('arguments[0].click();', searchbutton) # 只有不出现任何问题,才能继续,否则重新来一遍 bMutex.unlock() break except Exception as e: bMutex.unlock() traceback.print_exc() print('try again--------------------') try: # 等5秒,如果还没搜出来结果,那么肯定就是没有了,返回None time.sleep(2) bMutex.lock() idDom = wait.WebDriverWait(driver0, 3).until( EC.presence_of_element_located((By.ID, 'currentThreadSenderId'))) current = idDom.get_attribute('value') bMutex.unlock() return current except Exception as e: bMutex.unlock() print('No result found, ready to send message') return None
def run(self): assert isinstance(self.window,MyMainWindow) self.window.s.informationSignal.emit('wait!,','Please wait for a moment') bMutex.lock() try: if self.par.browserShowFlag == True: self.window.driver.set_window_position(-2000, 0) self.window.driver.set_window_size(1000, 1000) self.par.browserShowFlag = False elif self.par.browserShowFlag == False: self.window.driver.set_window_position(0, 0) self.window.driver.maximize_window() self.par.browserShowFlag = True # self.window.browserMutex.unlock() bMutex.unlock() except: bMutex.unlock() traceback.print_exc() print('browser page wrong!')
def run(self): try: bMutex.lock() self.window.modelText = self.window.getModelInputWidget( ).toPlainText() self.window.driver = webdriver.Firefox( executable_path=self.window.driverPath, firefox_profile=self.window.profile) self.window.setDriver(self.window.driver) self.window.driver.get(self.window.selectDateUrl) bMutex.unlock() try: with open('searchAllDate.temp', 'rb') as f: self.window.orderList = pickle.load(f) self.window.dateList = pickle.load(f) lastOrders = [] lastCurs = [] with open('searchSentID.txt', 'r') as f: while True: t = f.readline() if t == '': break else: l = t.split('#') lastOrders.append(l[0]) lastCurs.append(l[1]) except: self.window.s.informationSignal.emit('information', "can't find data files!") return time.sleep(30) # self.window.orderList, self.window.dateList = tool.getlist(self.window.driver) self.window.currentThreadSenderList = [] self.window.nameList = [] orderSizeStr = str(len(self.window.orderList)) self.window.s.scheduleSignal.emit(str(0) + r'/' + orderSizeStr) completedIndex = 0 # 拿这些ID去搜索 bMutex.lock() self.window.driver.get(self.window.getThreadUrl) bMutex.unlock() time.sleep(3) lastCurrent = 0 lastOrder = 0 abnormalID = [] self.window.currentThreadSenderList.extend(lastCurs) for i, index in zip(self.window.orderList, range(0, len(self.window.orderList))): sendIDText = open('searchSentID.txt', 'a') haveDonw = False if i in lastOrders: haveDonw = True get = lastCurs[index] # sendIDText = open('searchSentID.txt', 'a') while True: if haveDonw == True: break get = tool.getcurrent2(self.window.driver, i, lastcurrentid=lastCurrent, lastorderid=lastOrder) if get == None: # 说明没搜索到呀,那么就要给他发信 send.sendMessage2(send.generateSendMessageUrl(i), self.window.modelText, self.window.driver, i) elif get == 'abnormal': self.window.currentThreadSenderList.append('unknown') abnormalID.append(i) lastOrder = i lastCurrent = get break else: # 说明搜索到了,那么这个信件就不用重新发了 self.window.currentThreadSenderList.append(get) lastOrder = i lastCurrent = get break if haveDonw == False: sendIDText.write(i + '#' + get + '\n') sendIDText.close() self.window.s.addLogItemSignal.emit( 'Order ID:' + str(i) + '\nCurrentThreadSenderID:' + str(get)) completedIndex = completedIndex + 1 self.window.s.scheduleSignal.emit( str(completedIndex) + r'/' + orderSizeStr) tool.writeExcelAndAbnormalId(self.window.currentThreadSenderList, self.window.orderList, self.window.dateList, abnormalID) self.window.driver.close() try: os.remove('searchSentID.txt') os.remove('searchAllDate.temp') except: pass self.window.s.informationSignal.emit('information', 'completed successfully') self.window.s.ungraySignal.emit() except Exception as e: bMutex.unlock() traceback.print_exc() self.window.s.errorSignal.emit()
def run(self): try: bMutex.lock() self.window.modelText = self.window.getModelInputWidget( ).toPlainText() self.window.driver = webdriver.Firefox( executable_path=self.window.driverPath, firefox_profile=self.window.profile) self.window.setDriver(self.window.driver) self.window.driver.get(self.window.selectDateUrl) bMutex.unlock() time.sleep(3) self.window.orderList, self.window.dateList = tool.getlist( self.window.driver) infoFile = open('searchAllDate.temp', 'wb') pickle.dump(self.window.orderList, infoFile) pickle.dump(self.window.dateList, infoFile) infoFile.close() self.window.currentThreadSenderList = [] self.window.nameList = [] orderSizeStr = str(len(self.window.orderList)) self.window.s.scheduleSignal.emit(str(0) + r'/' + orderSizeStr) completedIndex = 0 # 拿这些ID去搜索 bMutex.lock() self.window.driver.get(self.window.getThreadUrl) bMutex.unlock() time.sleep(3) t = open('searchSentID.txt', 'w') t.close() lastCurrent = 0 lastOrder = 0 abnormalID = [] for i in self.window.orderList: sendIDText = open('searchSentID.txt', 'a') while True: get = tool.getcurrent2(self.window.driver, i, lastcurrentid=lastCurrent, lastorderid=lastOrder) if get == None: # 说明没搜索到,那么就要给他发信 send.sendMessage2(send.generateSendMessageUrl(i), self.window.modelText, self.window.driver, i) elif get == 'abnormal': self.window.currentThreadSenderList.append('unknown') abnormalID.append(i) lastOrder = i lastCurrent = get break else: # 说明搜索到了,那么这个信件就不用重新发了 self.window.currentThreadSenderList.append(get) lastOrder = i lastCurrent = get break sendIDText.write(i + '#' + get + '\n') sendIDText.close() self.window.s.addLogItemSignal.emit( 'Order ID:' + str(i) + '\nCurrentThreadSenderID:' + str(get)) completedIndex = completedIndex + 1 self.window.s.scheduleSignal.emit( str(completedIndex) + r'/' + orderSizeStr) tool.writeExcelAndAbnormalId(self.window.currentThreadSenderList, self.window.orderList, self.window.dateList, abnormalID) self.window.driver.close() try: os.remove('searchSentID.txt') os.remove('searchAllDate.temp') except: pass self.window.s.informationSignal.emit('information', 'completed successfully') self.window.s.ungraySignal.emit() except Exception as e: bMutex.unlock() traceback.print_exc() self.window.s.errorSignal.emit()
def sendMessage(sendMessageUrl, modelStr, driver, orderid): # 循环的目的是为了在发生未知错误时,能够重新进行一次 while True: try: # 选择subject为order Infomation print('start to send') bMutex.lock() driver.get(sendMessageUrl) bMutex.unlock() time.sleep(2) bMutex.lock() selectButtonWait = wait.WebDriverWait(driver, 10).until( EC.visibility_of_element_located( (By.ID, 'commMgrCompositionSubject'))) selectSubject = Select( driver.find_element_by_id('commMgrCompositionSubject')) selectSubject.select_by_index(1) # 获取名字 twoNameDiv = driver.find_elements_by_css_selector( "[style='padding-bottom:2px']") name = twoNameDiv[0].text.strip() p = re.compile(r':(.*)\(') t = re.search(p, name) name = t.group(1) # 获取订单详情 productname = driver.find_element_by_css_selector( "[style='list-style-position:inside; padding-left:0; margin-left:0']" ).text bMutex.unlock() if len(productname) > 80: productname = tool.getorderinfo2(driver, orderid) # 对model进行智能处理 patternOrderid = re.compile(r'#orderid') patternUsername = re.compile(r'#username') patternProductname = re.compile(r'#productname') modelStr = re.sub(patternOrderid, orderid, modelStr) modelStr = re.sub(patternUsername, name, modelStr) modelStr = re.sub(patternProductname, productname, modelStr) modelStr = '%r' % modelStr # 发送的内容 # modelStr = 'dear '+name+':\n'+modelStr bMutex.lock() wait.WebDriverWait(driver, 10).until( EC.visibility_of_element_located( (By.ID, 'commMgrCompositionMessage'))) textArea = driver.find_element_by_id('commMgrCompositionMessage') driver.execute_script("arguments[0].value=" + modelStr + ";", textArea) bMutex.unlock() time.sleep(1) # 点击发送邮件按钮 bMutex.lock() wait.WebDriverWait(driver, 10).until( EC.presence_of_all_elements_located( (By.CSS_SELECTOR, '#sendemail_label'))) allSendMailLabel = driver.find_elements_by_css_selector( '#sendemail') allSendMailLabel = allSendMailLabel[1] driver.execute_script("arguments[0].click();", allSendMailLabel) # 点了发送休息2S # time.sleep(3) wait.WebDriverWait(driver, 10).until( EC.presence_of_element_located( (By.NAME, 'Back to Manage Orders'))) bMutex.unlock() print('send OK') return name except Exception as e: bMutex.unlock() traceback.print_exc() print('try send again----------------------')
def getlist(driver0): allorderlist = [] alldatetimelist = [] # allorderinfolist = [] currentpagination = 0 onepageflag = 0 bMutex.lock() wait.WebDriverWait(driver0, 10000000).until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "select[name='itemsPerPage']"))) selectPagPer = select.Select( driver0.find_elements_by_name('itemsPerPage')[-1]) selectPagPer.select_by_value('100') # assert isinstance(driver0, webdriver.Chrome) goButton = driver0.find_elements_by_css_selector( "input[type='image'][width='21'") goButton = goButton[-1] goButton.click() bMutex.unlock() time.sleep(5) while True: # 最外层循环用来翻页 # 获取当前页数的循环,如果发生了不可预知的错误,那么重新进行 while True: try: time.sleep(2) bMutex.lock() orderlisthtml = wait.WebDriverWait(driver0, 10000000).until( EC.presence_of_element_located((By.ID, 'myo-table'))) bMutex.unlock() # 如果是刚开始,那么判断一下是不是只有一页(因为一页的时候网页排版不同) if currentpagination == 0: time.sleep(3) bMutex.lock() pagefulltr = driver0.find_element_by_xpath( "//div[@id='myo-table']/table/tbody/tr[1]") bMutex.unlock() if 'of' not in pagefulltr.text: onepageflag = 1 print('onegage!') break bMutex.lock() currentpagination1 = driver0.find_element_by_xpath( "//strong[@class = 'currentpagination']").text bMutex.unlock() if int(currentpagination1) == int(currentpagination) + 1: break except Exception as e: bMutex.unlock() traceback.print_exc() print('try again---------------------------------') # 匹配 while True: try: bMutex.lock() orderlisthtml = wait.WebDriverWait(driver0, 10000000).until( EC.presence_of_element_located((By.ID, 'myo-table'))) orderlist = [] datelist = [] timelist = [] allordertr = driver0.find_elements_by_xpath( "//div[@id='myo-table']/table/tbody/tr[contains(@id,'row-')]" ) for i in allordertr: orderid = str(i.get_attribute('id'))[-19:] if re.match(re.compile('\d{3}-\d{7}-\d{7}'), orderid) == None: continue orderlist.append(orderid) datetd = i.find_element_by_xpath("./td[2]") thisdate = str(datetd.text).split('\n') datelist.append(thisdate[0]) timelist.append(thisdate[1][:-4]) print("allorderget:" + str(time.clock())) bMutex.unlock() # 把这一页的信息加入 allorderlist.extend(orderlist) print("alldateget:" + str(time.clock())) for i, j in zip(datelist, timelist): thisdatetime = datetime.strptime(i + ' ' + j, "%b %d, %Y %I:%M:%S %p") alldatetimelist.append(thisdatetime) # 信息加入成功,那么可以离开这一页的try循环 # todo:这一页成功了,那么记录成功页到文件中即可 break except Exception as e: bMutex.unlock() traceback.print_exc() print('try again----------------------------') # 翻页 if onepageflag == 1: break try: bMutex.lock() nextpagebutton = driver0.find_element_by_xpath( "//a[text()='Next' and @class = 'myo_list_orders_link']") currentpagination = driver0.find_element_by_xpath( "//strong[@class = 'currentpagination']").text nextpagebutton.click() bMutex.unlock() print("next") except Exception as e: bMutex.unlock() # traceback.print_exc() print("finish to get orderID---------------------") print("finish:" + str(time.clock())) break return allorderlist, alldatetimelist
def getcurrent2(driver0, orderid, lastcurrentid, lastorderid): abnormity = 0 while True: try: bMutex.lock() wait.WebDriverWait(driver0, 5).until( EC.presence_of_element_located((By.ID, 'search-text-box'))) idinput = driver0.find_element_by_id("search-text-box") searchbutton = driver0.find_element_by_name("Search") driver0.execute_script("arguments[0].value=" + "'" + orderid + "'", idinput) driver0.execute_script('arguments[0].click();', searchbutton) # searchbutton.click() # 只有不出现任何问题,才能继续,否则重新来一遍 bMutex.unlock() break except Exception as e: bMutex.unlock() traceback.print_exc() print('try again--------------------') while True: try: bMutex.lock() time.sleep(1) idDom = wait.WebDriverWait(driver0, 3).until( EC.presence_of_element_located( (By.ID, 'currentThreadSenderId'))) current = str(idDom.get_attribute('value')) bMutex.unlock() if current == lastcurrentid: try: bMutex.lock() orderidA = wait.WebDriverWait(driver0, 3).until( EC.presence_of_element_located(( By.CSS_SELECTOR, "a[href^='/gp/orders-v2/details?ie=UTF8&orderID=']" ))) thisorderid = str(orderidA.get_attribute('href')[-19:]) bMutex.unlock() if thisorderid == orderid: bMutex.lock() idDom = wait.WebDriverWait(driver0, 3).until( EC.presence_of_element_located( (By.ID, 'currentThreadSenderId'))) current = str(idDom.get_attribute('value')) bMutex.unlock() return current else: abnormity += 1 print("abnormal:" + str(abnormity)) if abnormity > 10: print("Abnormal!!!!!!!!") return 'abnormal' except Exception as e0: bMutex.unlock() abnormity += 1 print("abnormal2:" + str(abnormity)) if abnormity > 10: print("Abnormal!!!!!!!!") return 'abnormal' else: return current except Exception as e: # traceback.print_exc() try: wait.WebDriverWait(driver0, 1).until( EC.presence_of_element_located( (By.CLASS_NAME, 'click-thread'))) bMutex.unlock() except Exception as ee: bMutex.unlock() print('No result found, ready to send message') return None