Verifying text is present within certain element Verifying text is present within certain element selenium selenium

Verifying text is present within certain element


I disagree to a degree. Badly constructed xpaths that search through the entire DOM will slow down tests but well constructed xpaths should have minimal effect on speed (unless you are using IE which has a horrific JavaScript rendering engine).

Ideally you want to key your xpath to an ID as close to the area of the DOM you want to search as possible, this will ensure you are only searching a specific area of the DOM rather than using an xpath like the one shown above that will search through the entire DOM even it it does find a matching element quickly.

I'll provide some examples of what I would call good xpaths using http://www.lazeryattack.com as an example. If you have anything specific in mind shout and I'll see what I can do to help:

  • The Voice Comms link: //ul[@id='leftMenu']/li/a[.='Voice Comms']
  • H3 element that contains a span element with the text "January VAT Increase": //div[@id='news']/h3[span[.='January VAT Increase']]
  • The first paragraph of text under the above element: //div[@id='news']/h3[span[.='January VAT Increase']]/following-sibling::p[1]
  • The second paragraph of text under the above element: //div[@id='news']/h3[span[.='January VAT Increase']]/following-sibling::p[2]
  • Start searching from the featuredNews div and drop down to the h2 element with the text "New Look And Feel": //div[@id='featuredNews']/descendant::h2[.='New Look And Feel']

I would suggect using FireFox with the FireBug and FirePath extensions to help you work out xpaths, this is my personal favourite combination.


Did you try

//ul[@class='sitemap']

Notice the single quote around sitemap.XPath would make your tests slow and id, name are better option to use.