How to write a regular expression for changing index

Best practices, code snippets for common functionality, examples, and guidelines.
RekhaG
Posts: 17
Joined: Fri Sep 22, 2017 12:47 am

How to write a regular expression for changing index

Post by RekhaG » Thu Sep 28, 2017 12:50 am

Hello there,

I am validating on logo of the website, but the index of the dom keeps changing which is causing my test case to fail. Can some one tell how to write a regular expression for a changing index. I browsed through the examples of regular expression in your website but could not find an appropriate solution for my problem.

Example Xpath: /dom[11]//div[#'headleft']?/?/img[@alt='Bio-Rad logo']

In the above example dom[11] might become dom[5] and so on. How can i avoid this problemt

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to write a regular expression for changing index

Post by odklizec » Thu Sep 28, 2017 7:00 am

Hi,

I think that using regex for index number in DOM is useless. You can the same remove the index completely from the xpath:

Code: Select all

/dom//div[#'headleft']?/?/img[@alt='Bio-Rad logo']
What you should do is to add a meaningful attribute to the xpath, like @domain. Otherwise, the search for elements could take a lot of time, because there could be numerous DOMs and Ranorex will have to search them all, until it finds matching element. So in my opinion, it's critical to set a meaningful attribute for DOM element.
Last edited by odklizec on Thu Sep 28, 2017 7:04 pm, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

RekhaG
Posts: 17
Joined: Fri Sep 22, 2017 12:47 am

Re: How to write a regular expression for changing index

Post by RekhaG » Thu Sep 28, 2017 6:19 pm

Thank you so much odklizec!! After removing the index it worked fine