2011-11-05 85 views
1

我一直在嘗試約一個星期來找出使用硒2的下拉菜單。這是一個項目,我正在努力使用ITA矩陣2自動化的航班搜索(http:///matrix.itasoftware.com/)。除了從下拉菜單中選擇乘客人數外,一切正常。點擊它可以正常工作,但是嘗試將密鑰或箭頭命令發送給它以及彈出時創建的新ID不會導致任何操作。我感謝任何幫助! (我對python非常陌生,我將這個項目作爲一種自我教學練習來學習)。與硒2的下拉菜單2

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

+1 matrix.itasoftware.com – Anders

+0

你有沒有與矩陣運氣?我正在做同樣的事情... –

回答

2

在這種情況下,由於乘客下拉菜單失敗實際上不是一個下拉菜單,它是一些聰明的HTML和JavaScript,從而預計它不會對一般的事件作出響應。在ID 一下就行了:

試試這個

ita_form_pax_Passenger_0 

然後單擊XPath的菜單項(與乘客人數替換 'NUMBER_OF_PASSENGERS'):

//td[contains(@id,'dijit_MenuItem')][text()='NUMBER_OF_PASSENGERS'] 
+0

有點晚回到你身邊(只是合併了一箇舊帳戶)。但我相信這在當時有幫助,我想感謝你。 – cmacdona101

+0

沒問題,很高興我能幫忙。 – Anders