2011-02-07 24 views
5

下面是我的腳本,它的檢查是元素存在。當我把這個選擇:在Selenium IDE的Selenium-python中的css選擇器中括號[?]的問題

css=input[name='flightSearchParam[3].originAirport'] 

就找我這個元素,但是當我在硒運行這個RC不能找到它。我認爲這是括號內的問題。

我必須改變以通過硒rc找到這個元素?

我在Windows XP和波蘭文化運行

腳本就可以運行。

# -*- coding: utf-8 -*- 
from selenium import selenium 
import unittest, time, re 

class Untitled(unittest.TestCase): 
    def setUp(self): 
     self.verificationErrors = [] 
     self.selenium = selenium("localhost", 5555, "*chrome", "http://www.aa.com/") 
     self.selenium.start() 


def test_untitled(self): 
    sel = self.selenium 
    sel.open("/international/internationalSplashAccess.do?countryCodeForIP=PL") 
    sel.click("localeSubmit") 
    sel.wait_for_page_to_load("30000") 
    for i in range(60): 
     try: 
      if sel.is_element_present("aa-hp-multi-city-link2"): break 
     except: pass 
     time.sleep(1) 
    else: self.fail("time out") 
    sel.click("aa-hp-multi-city-link2") 
    sel.click("flightSearchForm.button.reSubmit") 
    sel.wait_for_page_to_load("30000") 

    for i in range(60): 
     try: 
      if sel.is_element_present(u"css=input[name='flightSearchParam[3].originAirport']"): break 
     except: pass 
     time.sleep(1) 
    else: self.fail("time out") 

def tearDown(self): 
    self.selenium.stop() 
    self.assertEqual([], self.verificationErrors) 

if __name__ == "__main__": 
    unittest.main() 

機構:

conn.request("POST", "/selenium-server/driver/", body, headers) 

u'cmd=isElementPresent&1=css%3Dinput%5Bname%3DflightSearchParam%5B2%5D.originAirport%5D&sessionId=02b3341a3fee46f5a1e6d9c13d6e8916' 

編輯

我將其更改爲sel.is_element_present("dom=document.getElementsByName('flightSearchParam[3].originAirport')[0]"):

,並發現這個元素。但是,我仍然不知道爲什麼css在這裏不起作用:/

+0

你可以發佈您的html代碼,你想匹配 – karlcow 2011-02-11 04:53:40

+0

我認爲你會更好地使用除CSS以外的選擇器(XPath,也許?) – rs79 2011-02-11 13:45:14

回答

2

如果HTML代碼

<input name="flightSearchParam[3].originAirport"> 

那麼對於它的CSS選擇器會

css=input[name='flightSearchParam\[3\].originAirport'] 

你必須逃離這對於CSS選擇具有特定含義的支架。

0

看來RC並沒有翻譯轉義序列。我會建議使用XPath屬性。你的情況這將是 -

sel.is_element_present("//input[@name='flightSearchParam[3].originAirport']") 
1

我有可能獲得你一些有識之士對你的問題的解決方案類似的問題:

火狐硒IDE錄製期間返回這個目標css=input[name="keywords"]

正確CSS選擇參數,以獲得這個元素被證明是(硒2.41):

solution = driver.find_element_by_css_selector('input[name="keywords"]')

所以,你的情況,這可能工作:

css= 'input[name="flightSearchParam[3].originAirport"]' 
solution = driver.find_element_by_css_selector(css) 

注:在Python硒,我從來沒有需要逃避括號這表明指數...