我已经尝试了大约一个星期来找出使用selenium 2的下拉菜单。这是我正在努力使用ITA Matrix 2自动化航班搜索的项目(http://matrix.itasoftware.com/ )。除了从下拉菜单中选择乘客人数外,一切正常。单击它可以正常工作,但尝试向它发送键或箭头命令,以及弹出时创建的新ID不会导致任何操作。我感谢任何帮助! (我是python的新手,我正在做这个项目作为一种自学教学练习)。
from selenium import webdriver
from time import sleep
import time
driver = webdriver.Firefox()
driver.get("http://matrix.itasoftware.com/")
driver.find_element_by_id("advancedfrom1").send_keys(lport) #starting airport
driver.find_element_by_id("advancedto1").send_keys(rport) #destination airport
driver.find_element_by_id("advanced_rtDeparture").send_keys(ldate) #leaving date
driver.find_element_by_id("advanced_rtReturn").send_keys(rdate) #return date
driver.find_element_by_id("ita_form_location_RouteLanguageTextBox_0").send_keys(lflight) #going flight number
driver.find_element_by_id("ita_form_location_RouteLanguageTextBox_1").send_keys(rflight) #return flight number
#problem code:
driver.find_element_by_id("ita_form_pax_Passenger_0").click()#.send_keys("2")
time.sleep(2) #allow drop down to pop up
from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("dijit_MenuItem_4_text").send_keys(Keys.DOWN) #tried to scroll down
driver.find_element_by_id("ita_form_pax_Passenger_0").send_keys(Keys.DOWN)
#also tried just sending the number directly:
driver.find_element_by_id("dijit_MenuItem_4_text").send_keys(numpax)
driver.find_element_by_id("ita_form_pax_Passenger_0").send_keys(numpax)
答案 0 :(得分:2)
在这种情况下,它失败了,因为乘客下拉菜单实际上不是一个下拉菜单,它是一些聪明的HTML和JavaScript,所以它不会像预期的那样响应通常的事件。
试试这个: 点击ID:
上的列表ita_form_pax_Passenger_0
然后点击XPath上的菜单项(将“NUMBER_OF_PASSENGERS”替换为乘客人数):
//td[contains(@id,'dijit_MenuItem')][text()='NUMBER_OF_PASSENGERS']