<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或class="seat"至class="seat reserved selected 以下是我需要帮助实施的内容:从第1行开始,一直沿对于前两名乘客,请单击下一个可用的婴儿座位(每个婴儿座位一名乘客)对于最后两名乘客,请单击第1行开始的任意座位以后我们不需要实际选择一位乘客,因为他们会在选择座位后自动被选中,并且在加载页面时已经选择了第一位乘客.像我这样使用Python的人只有4天了,所以几乎没有高级人士知道如何实现上述功能吗?解决方案想法是:根据乘客是否有婴儿(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()Below I have four radio buttons that each select a passenger. <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">Below is a block of code that shows a row of seats (in row 6), some infants seats (class="seat infant") and the others normal seats (normal seats are seats noinfant - class="seat noinfant" and seats 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>When a seat is selected, the below happens:Class changes from class="seat infant" to class="seat infant reserved selected for infant seatsNormal Seats - class="seat noinfant" to class="seat noinfant reserved selected OR class="seat" to class="seat reserved selected Below is what I need help implementing:Starting from Row 1 and continuing alongFor the first two passengers, click the next available infantseats (one passenger per infant seat)For the last two passengers, click any seat starting from row 1onwardsWe don't need to actually select a passenger as they get selected automatically after selecting a seat, and first passenger is already selected when page is loaded.Does anybody as I have only used Python for 4 days so no way near advanced know how to implement the above? 解决方案 The idea would be:depending on whether a passenger has an infant or not (the data-hasinfant attribute), decide which seat to pickuse CSS class selectors with not() to get to the non-selected and non-reserved seatsSample implementation: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() 这篇关于如何遍历并单击相关项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 05:27