Pindakaas
Pindakaas

Reputation: 4439

how to find element in protractor test?

Trying to check the value of this element on an angular plunker:

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:(');
})    

Getting an error though:

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.

Is there a better way to find this span element or what is the right xpath expression?

Upvotes: 0

Views: 1161

Answers (2)

yTko
yTko

Reputation: 175

Try to use element(by.css('some css selector'))

Also, you have a typo in code - getText; instead of getText();

Or try this:

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

Upvotes: 0

Thomas
Thomas

Reputation: 181735

I think you meant By.cssSelector(...).

By.xpath expects an XPath expression, which would be something like /body/div/h2/span[2], although I'm not sure about the exact syntax.

By the way, your code would be easier to test if you added an ID or CSS class to the element, so you don't need to depend on the exact DOM structure.

Upvotes: 2

Related Questions