Selenium python中css选择器中的方括号[?]存在问题
下面是我的脚本,检查元素是否存在。当我给这个选择器:Selenium python中css选择器中的方括号[?]存在问题,python,css,selenium,selenium-rc,Python,Css,Selenium,Selenium Rc,下面是我的脚本,检查元素是否存在。当我给这个选择器: css=input[name='flightSearchParam[3].originAirport'] 在Selenium Ide中,它可以找到这个元素,但当我在Selenium rc中运行它时,它找不到它。我认为这是括号的问题 要通过selenium rc查找此元素,我必须更改什么 我在Windows XP和波兰文化下运行它 脚本已准备好运行 # -*- coding: utf-8 -*- from selenium import se
css=input[name='flightSearchParam[3].originAirport']
在Selenium Ide中,它可以找到这个元素,但当我在Selenium rc中运行它时,它找不到它。我认为这是括号的问题
要通过selenium 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\u present(“dom=document.getElementsByName('flightSearchParam[3].originAirport')[0]”:
它找到了这个元素。但是,我仍然不知道为什么css在这里不起作用:/尝试用反斜杠转义括号。如果HTML代码是
<input name="flightSearchParam[3].originAirport">
您必须转义对CSS选择器具有特定含义的括号。看来RC不会翻译转义序列。我建议使用XPath属性。就你的情况而言,是—
sel.is_element_present("//input[@name='flightSearchParam[3].originAirport']")
我有一个类似的问题,可能会让您对问题的解决方案有所了解: Firefox Selenium IDE在录制过程中返回此目标:
css=input[name=“keywords”]
获取此元素的正确CSS选择器参数为(selenium 2.41):
solution=driver。通过css\u选择器(“输入[name=“keywords”]”查找元素
因此,在您的情况下,这可能会起作用:
css= 'input[name="flightSearchParam[3].originAirport"]'
solution = driver.find_element_by_css_selector(css)
注意:在Python Selenium中,我从不需要转义表示索引的括号…u“css=input[name='flightSearchParam[3/].originAirport'/]”不起作用,对不起,我是说sel.u元素是否存在(u“css=input[name='flightSearchParam[3].originAirport']):不起作用。你能发布你想要匹配的html代码吗?我想你最好使用CSS以外的选择器(可能是XPath?)
css= 'input[name="flightSearchParam[3].originAirport"]'
solution = driver.find_element_by_css_selector(css)