2016-02-09 34 views
1

下面我有四個單選按鈕,每個選擇一個乘客。如何循環並點擊相關項目

<input checked="checked" name="passenger" id="passenger-0" data-passengerindex="0" type="radio"> 
    <input checked="checked" name="passenger" id="passenger-1" data-passengerindex="1" type="radio"> 
    <input checked="checked" name="passenger" id="passenger-2" data-passengerindex="2" type="radio"> 
    <input checked="checked" name="passenger" id="passenger-3" data-passengerindex="3" type="radio"> 

下面是一個代碼塊,顯示的座(第6行)的行,一些嬰兒座椅(class="seat infant")和其他正常座(正常座椅座位noinfant - class="seat noinfant"和座椅class="seat"

<td> 
     <a href="#6A" title="Seat Suitable for Infants 6A" data-seat="6A" data-seatid="81374518" data-seatgroup="0" class="seat infant" data-personid="" data-infantseat="True"> 
      <span>Seat Suitable for Infants 6A</span> 
     </a> 
    </td><td> 
     <a href="#6B" title="Seat Suitable for Infants 6B" data-seat="6B" data-seatid="81374519" data-seatgroup="0" class="seat infant" data-personid="" data-infantseat="True"> 
      <span>Seat Suitable for Infants 6B</span> 
     </a> 
    </td> 
    <td> 
     <a href="#6C" title="Seat Suitable for Infants 6C" data-seat="6C" data-seatid="81374520" data-seatgroup="0" class="seat infant" data-personid="" data-infantseat="True"> 
      <span>Seat Suitable for Infants 6C</span> 
     </a> 
    </td> 
     <td class="aisle">6</td> 
    <td> 
     <a href="#6D" title="Seat Suitable for Infants 6D" data-seat="6D" data-seatid="81374522" data-seatgroup="1" class="seat noinfant" data-personid="" data-infantseat="True"> 
      <span>Seat Suitable for Infants 6D</span> 
     </a> 
    </td> 
    <td> 
     <a href="#6E" title="Seat Suitable for Infants 6E" data-seat="6E" data-seatid="81374523" data-seatgroup="1" class="seat" data-personid="" data-infantseat="True"> 
      <span>Seat Suitable for Infants 6E</span> 
     </a> 
    </td> 
    <td> 
     <a href="#6F" title="Seat Suitable for Infants 6F" data-seat="6F" data-seatid="81374524" data-seatgroup="1" class="seat infant" data-personid="" data-infantseat="True"> 
      <span>Seat Suitable for Infants 6F</span> 
     </a> 
    </td> 
</tr> 

當選擇了一個座位,下面的情況發生:

  • 類從class="seat infant"變化class="seat infant reserved selected嬰兒座椅
  • 普通椅子 - class="seat noinfant"class="seat noinfant reserved selected OR class="seat"class="seat reserved selected

下面是我需要協助:

從第1行開始,沿着

繼續
  • 對於前兩位乘客,請單擊下一個可用嬰兒 個席位
  • 在過去的兩名乘客(每個嬰兒座椅一名乘客),點擊任何座位排從1 開始起

,因爲他們得到之後,自動選擇我們並不需要實際選擇乘客選擇座位,並且在加載頁面時已經選擇了第一位乘客。

有沒有人因爲我只用了4天的Python所以沒有辦法接近高級知道如何實現上述?

回答

1

的想法是:

  • 根據乘客是否有嬰兒或沒有(在data-hasinfant屬性),決定選擇哪個座位
  • 使用CSS類選擇與not()去非 - 請選擇無保留席位

樣品實施:

for passenger in driver.find_elements_by_css_selector("ol.passengerlist li[data-personid]"): 
    passenger.click() 

    has_infant = passenger.get_attribute("data-hasinfant") 

    # choose seats 
    if has_infant: 
     # select a non-selected infant seat 
     seat = driver.find_element_by_css_selector(".planebody a.seat.infant:not(.reserved):not(.selected)") 
    else: 
     # select a non-reserved non-selected seat 
     seat = driver.find_element_by_css_selector(".planebody a.seat:not(.reserved):not(.selected)") 

    print("Passenger: %s, choosing seat: %s" % (passenger.text.strip(), seat.get_attribute("data-seat"))) 
    seat.click()