selenium simple test说xpath不是一个合法的表达

时间:2013-09-18 01:52:04

标签: xpath selenium selenium-webdriver

我开始使用SST(selenium simple test)但在执行看似有效的xpath表达式'//div[div@data-type="folder-name"]'时遇到了问题。 SST因以下追溯而失败:

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/sst/cases.py", line 207, in run_test_script exec self.code in self.context
File "./sst-one.py", line 7, in <module> names = get_elements_by_xpath('//div[div@data-type="folder-name"]')
File "/usr/local/lib/python2.7/dist-packages/sst/actions.py", line 1344, in >get_elements_by_xpath_raise(msg)
File "/usr/local/lib/python2.7/dist-packages/sst/actions.py", line 118, in _raise raise AssertionError(msg)
AssertionError: Element not found: Message: u'The given selector //div[div@data->type="folder-name"] is either invalid or does not result in a WebElement. The following >error occurred:\nInvalidSelectorError: Unable to locate an element with the xpath >expression //div[div@data-type="folder-name"] because of the following >error:\n[Exception... "The expression is not a legal expression."  code: "12" nsresult: >"0x805b0033 (SyntaxError)"  location: "file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/driver_component.js> Line: 5916"]' ;
Stacktrace: 
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/driver_component.js:8873)
    at FirefoxDriver.prototype.findElementsInternal_ (file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/driver_component.js:8931)
    at FirefoxDriver.prototype.findElements file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/driver_component.js:8935)
    at DelayedCommand.prototype.executeInternal_/h file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/command_processor.js:10840)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/command_processor.js:10845)
    at DelayedCommand.prototype.execute/< file:///tmp/tmp12zCta/extensions/fxdriver@googlecode.com/components/command_processor.js:10787)

SST代码行是:

names = get_elements_by_xpath('//div[div@data-type="folder-name"]')

如果我使用selenium(来自python)而不是SST运行相同的xpath语句,那么它是一个有效的表达式并返回匹配的元素(如果有的话)。

纯selenium代码行(其中br是firefox webdriver对象)是:

elems = br.find_elements_by_xpath("//div[div/@data-type='folder-name']")

我明白这有点杂草,所以如果有人提示如何调试selenium在/ tmp目录中创建的.js代码,这将是一个很大的帮助。

3 个答案:

答案 0 :(得分:1)

它不是有效的XPath表达式。此

//div[div@data-type="folder-name"]

应该阅读

//div[div/@data-type="folder-name"]

这确实是你在一个例子中所拥有的。

答案 1 :(得分:0)

你没有显示任何标记,假设你有

<div><div data-type="folder-name"></div></div>

你需要:

elems = br.find_elements_by_xpath("//div//div[@data-type='folder-name']")

答案 2 :(得分:0)

如果您尝试使用data-type =“folder-name”获取所有可能的div标签,

//div[@data-type="folder-name"]

如果第一个div的子项只有data-type =“folder-name”,

//div[div/@data-type="folder-name"]