0

I want to write something like:

return component.element(by.xpath('//div/div/a')); // I want: path to component + xpath

But I always have the result same with:

return element(by.xpath('//div/div/a'));   // I always have: just xpath

Component is an element that was found with my own locator:

component = element(by.Name('Component_name'));

What I doing wrong?

  • Could you please also add information about what is a component. – Michael Radionov Aug 17 '15 at 13:31
  • @Michael Radionov I edited question. – Лилия Сапурина Aug 17 '15 at 14:07
  • 2
    There must be some Protractor documentation that is giving people the impression that XPath expressions starting with `//` are "relative paths." The questioner at http://stackoverflow.com/q/31884964/423105 has the some mistaken understanding. Can you tell us where you read about "relative XPaths" so we can get it corrected? Did you get this from http://seleniumeasy.com/selenium-tutorials/xpath-tutorial-for-selenium? – LarsH Aug 17 '15 at 14:48

1 Answers1

4

I guess it is because of the selector for xpath locator. When you use expression starting with //, it will look for any element on the page, even if you use it with parent element. There is a note about it in the docs for xpath locator:

For example, given the selector "//div", WebDriver will search from the document root regardless of whether the locator was used with a WebElement.

It can be fixed by putting a dot before double slash: .//div/div/a, which should make a search relative to parent element.

Take a look at xpath spec, to find more info.

Michael Radionov
  • 12,859
  • 1
  • 55
  • 72