无法使用python

时间:2019-09-30 12:56:18

标签: python selenium-webdriver datepicker

我想使用python硒在https://book.spicejet.com/Search.aspx上搜索

我查看了this个问题,但这不是我要找的正确答案。

我搜索了有2名成人和2名婴儿从加尔各答飞往果阿的航班。当我提供乘客详细信息时,我无法选择婴儿的出生日期。

import time
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import Select

browser = webdriver.Chrome()

booking_url = "https://book.spicejet.com/Search.aspx"
browser.get(booking_url)

departureButton = browser.find_element_by_id("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1_CTXT").click()

browser.find_element_by_partial_link_text("Kolkata").click()

arivalButton = browser.find_element_by_id("ControlGroupSearchView_AvailabilitySearchInputSearchViewdestinationStation1_CTXT")
arivalButton.click()
time.sleep(.3)
arivalButton.send_keys("Goa")
time.sleep(1)
search_date = "20-September 2019"
dep_date = search_date.split("-")

dep_month = dep_date[1]
dep_day = dep_date[0]

while browser.find_element_by_class_name("ui-datepicker-title").text != dep_month:
    browser.find_element_by_css_selector("a[title='Next']").click()

browser.find_element_by_xpath("//table//a[text()='"+dep_day+"']").click()
time.sleep(1)

try:
    return_date_close = browser.find_element_by_class_name("date-close").click
except:
    pass


pax_selct = browser.find_element_by_id("divpaxinfo").click()
time.sleep(.2)

# __________Adult number_____________

for i in range(0, 2 - 1):
    adults = browser.find_element_by_id("hrefIncAdt")
    adults.click()
# ____________Set Num of Children___________________
for i in range(0, 0):
    childrens = browser.find_element_by_id("hrefIncChd")
    childrens.click()
# ____________Set Num of Infant(s)___________________
for i in range(0, 2):
    infants = browser.find_element_by_id("hrefIncInf")
    infants.click()

donebttn = browser.find_element_by_id("btnclosepaxoption").click()
searchBtn = browser.find_element_by_class_name("bookbtn").click()

browser.switch_to.default_content()
flightarr = []
tbl_row = browser.find_elements_by_class_name("fare-row")
time_select=3
price_select=1
new_time_serial = 0
tr_cont = 4
for item in tbl_row:
      if item.is_displayed():
            if new_time_serial == time_select:
                  col = item
                  cont = str(tr_cont)
                  if price_select == 0:
                        price1 = col.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr['+cont+']/td[3]/p').click()
                  elif price_select == 1:
                        price2 = col.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr['+cont+']/td[4]/p').click()

            new_time_serial = new_time_serial + 1
      tr_cont = tr_cont + 1

time.sleep(1)
cntn_btn = browser.find_element_by_class_name("button-continue").click()
passen_serial = 0
passen_serial_inf = 0
#inf = 1

birth_year = "2017"
birth_month = "Nov"
birth_day = "30"

all_pass_frm = browser.find_element_by_class_name("multicontent")
all_pass_entry = all_pass_frm.find_elements_by_class_name("sectionContent")

for passen in all_pass_entry:
    pass_type = passen.find_element_by_class_name("guest-heading").text.split(' ',1)[0]
    pass_type2 = passen.find_element_by_class_name("guest-heading").text.split(' ',1)[1]

    if pass_type == "Adult":
        deg_sel_name = Select(passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_DropDownListTitle_" + str(passen_serial) + ""))
        deg_sel_name.select_by_index(1)

        first_name_in = passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_TextBoxFirstName_" + str(passen_serial) + "")
        first_name_in.send_keys("imam")

        last_name_in = passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_TextBoxLastName_" + str(passen_serial) + "")
        last_name_in.send_keys("Hossain")

    elif pass_type == "Child":

        deg_sel_name = Select(passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_DropDownListGender_" + str(passen_serial) + ""))
        deg_sel_name.select_by_index(2)

        first_name_in = passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_TextBoxFirstName_" + str(passen_serial) + "")
        first_name_in.send_keys("Korim")

        last_name_in = passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_TextBoxLastName_" + str(passen_serial) + "")
        last_name_in.send_keys("Hossain")

    elif pass_type == "Infant":
        deg_sel_name = Select(passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_DropDownListGender_"+ str(passen_serial_inf) + "_" + str(passen_serial_inf) + ""))
        deg_sel_name.select_by_index(2)

        first_name_in = passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_TextBoxFirstName_"+ str(passen_serial_inf) + "_" + str(passen_serial_inf) + "")
        first_name_in.send_keys("Aqiba")

        last_name_in = passen.find_element_by_id("CONTROLGROUPPASSENGER_PassengerInputViewPassengerView_TextBoxLastName_"+ str(passen_serial_inf) + "_" + str(passen_serial_inf) + "")
        last_name_in.send_keys("Hassan")

        dob = passen.find_element_by_id("inputDateContactInfant" +str(pass_type2)+ "").click()
        dob_cal = browser.find_element_by_class_name("datepickerViewYears")
        dob_cal_year = dob_cal.find_element_by_class_name("datepickerYears")
        inf_birth_year = dob_cal.find_element_by_xpath('.//*[@class="datepickerYears"]/tr/td/a/span[text()="'+birth_year+'"]').click()
        inf_birth_mon = dob_cal.find_element_by_xpath('.//*[@class="datepickerMonths"]/tr/td/a/span[text()="'+birth_month+'"]').click()
        inf_birth_day = dob_cal.find_element_by_xpath('.//*[@class="datepickerDays"]/tr/td/a/span[text()="'+birth_day+'"]').click()


    #   inf = inf +1 
        passen_serial_inf = passen_serial_inf + 1
    passen_serial = passen_serial + 1


print("Done")

我尝试使用xPath对第一个婴儿使用,但不适用于第二个婴儿。我现在该怎么办?除了XPath还有什么办法吗?如果乘客人数不同,我该怎么办?

1 个答案:

答案 0 :(得分:0)

我已经提取了var声明和var增量,声明声明中似乎存在不匹配,这可能会导致问题,var声明不需要以相同的数字开头吗?设置它们相等然后重试。


var声明


passen_serial_inf = 0


inf = 1


var增量


inf = inf +1


    passen_serial_inf = passen_serial_inf + 1

passen_serial = passen_serial + 1