如何在量角器测试中找到元素?

时间:2015-07-02 07:29:08

标签: angularjs protractor

尝试在角度探测器上检查此元素的值:

it('should display valid',function(){
  browser.get('http://embed.plnkr.co/3nt01z/preview');
  var errortext =element(By.xpath('body>div>h2>span:nth-child2)')).getText);
  expect(errortext).toBe('Not:(');
})    

虽然收到错误:

Stacktrace:
InvalidSelectorError: invalid selector: Unable to locate an element with the xpath expression body>div>h2>span:nth-child(2) because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'body>div>h2>span:nth-child(2)' is not a valid XPath expression.

是否有更好的方法来查找此span元素或什么是正确的xpath表达式?

2 个答案:

答案 0 :(得分:2)

我认为你的意思是By.cssSelector(...)

By.xpath需要一个XPath表达式,类似于/body/div/h2/span[2],尽管我对确切的语法不确定。

顺便说一下,如果你在元素中添加了ID或CSS类,你的代码会更容易测试,所以你不需要依赖于确切的DOM结构。

答案 1 :(得分:0)

尝试使用元素(by.css('某些css选择器'))

此外,你的代码中有拼写错误 - getText;而不是getText();

或试试这个:

var errortext = element(By.xpath('/body/div/h2/span[2]')).getText();