元素不可见错误(Python,Selenium)

时间:2016-02-13 19:49:34

标签: python python-2.7 selenium selenium-webdriver

我遇到一个问题,我的PYTHON GUI中出现错误,说明' ElementNotVisibleException:消息:元素当前不可见,因此可能无法与'进行交互,所以它是'停止我的剧本。

实际上我在下面有一张图片,我选择了一个座位选择了出境航班乘客的座位,选择了下一个单选按钮,然后选择了另一个座位并继续。

但是,在出境航班上为最后一位乘客选择座位后,它将自动沿着入站列表向下并选择第一个乘客的单选按钮,但脚本会因错误而停止。

在尝试点击入境旅客之前,我尝试使用xpath等待入站地图的平面地图显示,但它并不喜欢。

我的问题是,我怎样才能让脚本选择入境航班的座位,就像出境航班从出境航班转入入境航班一样?

以下是我选择乘客及其座位的代码:

#seats selection - outbound

for outbound_passenger in driver.find_elements_by_css_selector("ol[data-flightbound='Outbound'] li[data-personid]"):
    outbound_passenger.click()

#driver.find_elements_by_css_selector("ol.passengerlist li[data-personid]"):

    outbound_has_infant = outbound_passenger.get_attribute("data-hasinfant")

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

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

inbound_plan = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[3]/form/div[1]/div/div[1]/div[15]/div[2]/div/div[2]/div[2]")))

#seats selection - inbound

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

#driver.find_elements_by_css_selector("ol.passengerlist li[data-personid]"):

    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(".planebody a.seat.infant:not(.reserved):not(.selected)")
    else:
        # select a non-reserved non-selected seat
        inbound_seat = driver.find_element_by_css_selector(".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()

1 个答案:

答案 0 :(得分:2)

可能有多个inbound_seat,然后当您运行此代码时,Selenium返回的元素不是您期望的元素。请注意,默认情况下,Selenium始终返回第一个元素,以防有多个元素。

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