我已经尝试了大约一周的时间,以找出使用selenium2的下拉菜单。这是我正在使用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)
在这种情况下,它会失败,因为乘客下拉菜单实际上不是下拉菜单,它是一些巧妙的html和javascript,因此不会响应预期的正常事件。
尝试以下操作:单击ID上的列表:
ita_form_pax_Passenger_0
然后点击XPath上的菜单项(用乘客人数代替’NUMBER_OF_PASSENGERS’):
//td[contains(@id,'dijit_MenuItem')][text()='NUMBER_OF_PASSENGERS']