index += 1
导致脚本每次都跳过一个元素。因此它将打印出名称,汽车,apt_time,联系人,注释,然后不会转到下一个元素,它会跳过它并在其后打印一个,但如果我将索引设置为0,它只会重复名称,汽车,apt_time,联系,注意每个元素的第一个元素。
while 31 > today_date > 1:
# Find calender
div = browser.find_element_by_id('ledgerdateslider-1247-innerCt')
# hit calender
div.find_element_by_xpath('//*[@id="component-1250"]/div/table/tbody/tr/td[1]').click()
# hit calender date
browser.implicitly_wait(2)
browser.find_element_by_link_text(str(today_date)).click()
time.sleep(2)
try:
index = 0
while True:
header = browser.find_elements_by_class_name('x-ledgerdataview-header')
try:
time.sleep(2)
header[index].click()
while True:
name = browser.find_elements_by_class_name('x-lightcard-customer-name')
car = browser.find_elements_by_class_name('x-lightcard-vehicle-inf')
apt_time = browser.find_elements_by_class_name('x-lightcard-header-right')
contact = browser.find_elements_by_class_name('x-lightcard-contact')
note = browser.find_elements_by_class_name('x-lightcard-services')
try:
# print and write to file name/car/apt_time/contact/note
print(name[index].text)
f.write("\n" + 'Name: ' + name[index].text + "\n")
print(car[index].text)
f.write('Car: ' + car[index].text + "\n")
print(apt_time[index].text)
f.write('Time: ' + apt_time[index].text + "\n")
print(contact[index].text)
f.write('Contact: ' + contact[index].text + "\n")
print(note[index].text)
f.write('Service Notes:' + note[index].text + "\n")
print(today_date)
f.write('Date:' + str(today_date) + "\n")
time.sleep(3)
index += 1
break
except IndexError:
break