Selenium and Xpath

curiouswebster
curiouswebster used Ask the Experts™
on
In this Selenium test, can you explain what this call to By.XPath is doing exactly?

string referenceLink = browser.FindElement(By.XPath("/html/body/div[@class='container body-content']/div[@class='page-wrap']/div[@class='container']/div[@class='box-container jumbotron border']/div[@class='row'][1]/div[@class='col-sm-12 col-md-6 col-lg-3'][1]/a[@class='thumbnail text-center']")).GetAttribute("href").Remove(0, 8);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Quality Assurance
Commented:
It looks like this part :

  • /html/body/div[@class='container body-content']/div[@class='page-wrap']/div[@class='container']/div[@class='box-container jumbotron border']/div[@class='row'][1]/div[@class='col-sm-12 col-md-6 col-lg-3'][1]/a[@class='thumbnail text-center']

is starting at the top of the DOM, and then digging down to find a thumbnail.

This part:

  • .GetAttribute("href").Remove(0, 8);

is getting the href of the image element , then removing the first 8 chars of it.


Is the call not working for you any more?
curiouswebsterSoftware Engineer

Author

Commented:
I am just trying to understand it fully, that's all.

What does the By.XPath() do?
Bill TorchiaQuality Assurance
Commented:
In Selenium there are "By" objects. They are used a lot to find elements on the page. Basically they tell Selenium to go look for an element at a location, using a specific method. Docs are here : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html
curiouswebsterSoftware Engineer

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial