如何使用if语句检查某些内容是否可见?

时间:2016-02-14 00:52:26

标签: python python-2.7 selenium selenium-webdriver

我试图编写一个if语句,如果出现警告(inner_modal_visible),然后单击继续按钮(现在使用XPATH进行测试,但一旦我知道它就会将其更改为类)适用于xpath),否则继续点击inbound_seat.click()

如果我实际上删除了外部和if语句中的xpath继续按钮代码,那么它会选择所有inbound_seats,但显然我无法继续。如果我重新添加继续按钮代码,那么在选择一个inbound_seat后,我会收到以下错误。

我做错了什么以及如何让if语句生效,以便如果出现alear,则点击可点击的继续按钮,否则继续选择入站席位?

Traceback (most recent call last):
      File "C:\Users\mayur\Documents\Selenium\Return Flight - no products.py", line 314, in <module>
        wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/form/div[1]/div/div[1]/div[12]/div[2]/p[2]/button"))).click();
      File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
        raise TimeoutException(message, screen, stacktrace)
    TimeoutException: Message: 

代码:

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

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

modal_inner_visible = driver.find_element_by_id("AllInboundPassengersSeatedOk")

if modal_inner_visible.is_displayed():
    #confirm seats reserved
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/form/div[1]/div/div[1]/div[12]/div[2]/p[2]/button"))).click();

else:
    #continue selecting seats
    inbound_seat.click()

1 个答案:

答案 0 :(得分:1)

循环乘客后,等待相应Continue按钮的“可点击性”:

for inbound_passenger in driver.find_elements_by_css_selector("ol[data-flightbound='Inbound'] li[data-personid]"):
    inbound_passenger.click()
    inbound_has_infant = inbound_passenger.get_attribute("data-hasinfant")

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

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

# proceed to the next page
submit_seat_selection = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#AllInboundPassengersSeatedOk button.submitseatselection")))
submit_seat_selection.click()