如何循环浏览并点击相关项

时间:2016-02-09 22:43:43

标签: python python-2.7 selenium selenium-webdriver

下面我有四个单选按钮,每个按钮都选择一名乘客。

<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")以及其他正常座位(普通座位为座位无关 - class="seat noinfant"和座位{{ 1}}。

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 selectedclass="seat noinfant"class="seat noinfant reserved selectedclass="seat"

以下是我需要帮助实施的内容:

从第1行开始并继续

  • 对于前两位乘客,请点击下一位婴儿 座位(每个婴儿座位一名乘客)
  • 对于最后两位乘客,请单击从第1行开始的任何座位 起

我们不需要实际选择乘客,因为他们在选择座位后会自动选择,并且在加载页面时已经选择了第一位乘客。

我是否只使用了Python 4天,所以没有接近高级的人知道如何实现上述功能?

1 个答案:

答案 0 :(得分:1)

这个想法将是:

  • 取决于乘客是否有婴儿(data-hasinfant属性),决定选择哪个座位
  • 使用带有not()的CSS类选择器来到非选定和非预留席位

示例实施:

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()