我正在使用脚本来跟踪网站上的包。如果我运行我制作的脚本,有时包显示为"不幸的是,您输入的数字不在我们的跟踪系统中。",但是如果我输入并手动搜索它然后显示作为"已交付"。一个例子是跟踪号RN231029883CA。
我不确定这是否可能是由网站上某种类型的垃圾邮件过滤器引起的。我在网站源代码中看到了一些名为spamValidate()的内容,但不知道如何检查这是否是原因。
HTML代码:
Set Källkod = Worksheets("Källkod")
HomeTeamAbb = "MODO"
AwayTeamAbb = "ÖHK"
For y = LastRow To StartRow Step -1
Set cell = Källkod.Range("A" & y)
If cell Like "*(EQ*" And Not cell Like "*ENG*" Then
If cell Like "*" & HomeTeamAbb & "*" And InStr(InStr(1, cell, "(EQ") + 1, cell, HomeTeamAbb) > 0 Then
HomeGoalsEQ = HomeGoalsEQ + 1
ElseIf cell Like "*" & AwayTeamAbb & "*" And InStr(InStr(1, cell, "(EQ") + 1, cell, AwayTeamAbb) > 0 Then
AwayGoalsEQ = AwayGoalsEQ + 1
ElseIf Källkod.Range("A" & cell.row + 1) Like "*" & HomeTeamAbb & "*" Then
HomeGoalsEQ = HomeGoalsEQ + 1
ElseIf Källkod.Range("A" & cell.row + 1) Like "*" & AwayTeamAbb & "*" Then
AwayGoalsEQ = AwayGoalsEQ + 1
End If
If InStr(1, cell, "&") > 0 Then
CurrentGoalTime = Mid(Trim(cell), InStr(InStr(1, cell, "&") + 1, Trim(cell), ":") - 2, 5)
Else
CurrentGoalTime = Mid(Trim(cell), InStr(1, Trim(cell), ":") - 2, 5)
End If
End If
Next
我已经运行了几次这个脚本,它似乎不一致,何时发生这种情况,什么时候不发生。所以也许我需要另一个EC等待某个地方? (编辑:我怀疑这是EC等待问题,因为它发生在我使用调试并逐行进行时)这基本上是我在这个项目中需要克服的最后障碍。非常感谢任何帮助。
我的整个代码:
</div><form id="tapByTrackSearch:trackSearch" name="tapByTrackSearch:trackSearch" method="post" action="/cpotools/apps/track/personal/findByTrackNumber?execution=e1s1" enctype="application/x-www-form-urlencoded" onsubmit="return spamValidate();"><textarea name="tapByTrackSearch:trackSearch:trackNumbers" id="tapByTrackSearch:trackSearch:trackNumbers" tabindex="0">RN231029883CA,RN231029884CA,</textarea>
<!--p class="under-input-txt">Enter up to 24 numbers, separated by commas</p-->
<div class="row">
<div class="large-12 medium-12 small-12 columns"><input id="tapByTrackSearch:trackSearch:submit_button" name="tapByTrackSearch:trackSearch:submit_button" type="submit" value="Track" onclick="if(typeof window.clearFormHiddenParams_tapByTrackSearch_trackSearch=='function'){clearFormHiddenParams_tapByTrackSearch_trackSearch('tapByTrackSearch:trackSearch');}if(typeof window.getScrolling!='undefined'){oamSetHiddenInput('tapByTrackSearch:trackSearch','autoScroll',getScrolling());}" class="button margintop20 marginright" /><input type="hidden" name="autoScroll" />
<a href="#" id="showrecentlytrackeditemsnum" onClick="showRecentlyTrackedItems('byTrack', 'tapByTrackSearch:trackSearch:trackNumbers')" class="button light">Recent items</a>
</div>
</div><input type="hidden" name="tapByTrackSearch:trackSearch_SUBMIT" value="1" /><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="v1Dxx9hSieZwMVPzZZHAPSkIfrSZM1jbO4mNTTN2U0GpxoAFjzFhwEssO/Z0p/uzYyUiHf4E5tIMuqNSqP+dRYKpGsqhkV+2Tm+y5O2kdGHE2HH2c1AYdcgvvjRyYzcsTLGN8xKfqu9JfGmss6QW+w==" /></form>
</div>
我认为是问题的部分代码: 或RowCount2在范围内(0,CalcLoops):#QtySearch):
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from openpyxl import Workbook
import bs4 as bs
import math
book = Workbook()
sheet = book.active
i=0
PasteRowNum=1
DeleteTrackingNumbers=0
#Change this value to your starting tracking number
StartingTrackingNumber=231029883
#Change this value to increase or decrease the number of tracking numbers you want to search overal
TrackingNumberCount = 2
#Number of Tacking Numbers Searched at One Time
QtySearch = 2
for i in range(0,TrackingNumberCount):
g=i+StartingTrackingNumber
sheet.cell(row=i+1,column=1).value = 'RN' + str(g) + 'CA,'
TrackingNumbers = []
for col in sheet['A']:
TrackingNumbers.append(col.value)
MaxRow = sheet.max_row
MaxIterations = math.ceil(MaxRow / QtySearch)
RowCount = 0
LastTrackingThisPass = 0 #QtySearch
#Safari Search Loops
driver = webdriver.Safari()
driver.set_page_load_timeout(10)
for RowCount in range (0,MaxIterations):
FirstTrackingThisPass = (RowCount)*QtySearch
LastTrackingThisPass = FirstTrackingThisPass + QtySearch
print(FirstTrackingThisPass)
print(LastTrackingThisPass)
x = TrackingNumbers[FirstTrackingThisPass:LastTrackingThisPass]
print(x)
CalcLoops = len(x)
print(CalcLoops)
driver.get("https://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?execution=e1s1")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[contains(@id, "trackNumbers")]')))
if DeleteTrackingNumbers == 1:
driver.find_element_by_xpath('//*[contains(@id, "trackNumbers")]').clear() #send_keys((Keys.CONTROL, "a"), "55")
print("I tried")
driver.find_element_by_xpath('//*[contains(@id, "trackNumbers")]').send_keys(x)
driver.find_element_by_xpath('//*[contains(@id, "submit_button")]').send_keys(chr(13))
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".mobile_txt_holder, .notfoundcopy")))
SourceCodeTest = driver.page_source
DeleteTrackingNumbers=1
Soup = bs.BeautifulSoup(SourceCodeTest, "lxml") #""html.parser")
objects = []
for each in Soup.find_all("fieldset"):
each = each.get_text().split("\n") #split the ugly string up
each = [each[1][-13:], each[5]] #grab the parts you want, rmv extra words
objects.extend(each)
#If the sheet comes back with "You entered a valid number..." then perform loop one at a time
VaildSearchCheck = "You entered a valid number, but we are not able to show the tracking information for one of the following reasons: "
if VaildSearchCheck == objects[1]:
objects=[]
ThisNumber=FirstTrackingThisPass
for RowCount2 in range(0, CalcLoops): #QtySearch):
TN = TrackingNumbers[ThisNumber]
TNum = [TN[0:]]
#LastTrackingThisPass += QtySearch
print(TNum)
driver.get("https://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?execution=e1s1")
print(1)
driver.get_screenshot_as_file("Canada Post A1.png")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[contains(@id, "trackNumbers")]')))
try:
driver.find_element_by_xpath('//*[contains(@id, "trackNumbers")]').clear() # send_keys((Keys.CONTROL, "a"), "55")
print("aaa")
except NoSuchElementException:
driver.implicitly_wait(1)
print("bbb")
driver.find_element_by_xpath('//*[contains(@id, "trackNumbers")]').clear() # send_keys((Keys.CONTROL, "a"), "55")
print(2)
driver.find_element_by_xpath('//*[contains(@id, "trackNumbers")]').send_keys(x)
print(3)
driver.find_element_by_xpath('//*[contains(@id, "submit_button")]').send_keys(chr(13))
print(4)
ThisNumber += 1
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".mobile_txt_holder, .notfoundcopy")))
SourceCodeTest = driver.page_source
print(5)
Soup = bs.BeautifulSoup(SourceCodeTest, "lxml") # ""html.parser")
#print(Soup.prettify())
try:
#Find the Date:
Date=driver.find_element_by_class_name("mobile_txt_holder").text
Date=[Date[1:]]
print(Date)
print(1212)
except NoSuchElementException:
Date="x"
print(7777)
objects.extend(TNum)
objects.extend(Date)
print("Yellow")
###Can prob add an else statement to the above if statment to only perform if check worked....
objectsnum=0
Stop=int(len(objects)/2)
#print to excel loop
for r in range(0,Stop):
sheet.cell(row=r+PasteRowNum,column=1).value = objects[objectsnum]
sheet.cell(row=r+PasteRowNum,column=2).value = objects[objectsnum+1]
objectsnum+=2
#Excel File Name
book.save('First Run Test.xlsx')
#Keep Track of Row Number
PasteRowNum += QtySearch+1
print("Loop")
print("Success")
答案 0 :(得分:0)
我在代码中发现错误,因为我发送了密钥(x)而不是密钥(TN),因为在我开始清理代码后,我甚至无法让它间歇性地工作。
我确实每次都通过在发送密钥()
之前添加click()来使其工作代码:
trackNumbers = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[contains(@id, "trackNumbers")]')))
try:
trackNumbers.click()
trackNumbers.clear()