Home > Software engineering >  need help about writing xpath which contain dynamic values in ID attribute
need help about writing xpath which contain dynamic values in ID attribute

Time:10-01

HTML code:

<div id='abcd12$selenium450'>

where the numeric value ie 12 or 450 are changing frequently but the string part remains constant. I have tries with contains(), but I am confused bcz the xpath should contain abcd as well as selenium. Can anyone help here?

CodePudding user response:

You should use starts-with and contains both.

//div[starts-with(@id, 'abcd') and contains(@id,'selenium')]

CodePudding user response:

You can achieve that in multiple ways.

  • starts-with and contains CSS:
div[id^='abcd'][id*='selenium']
  • starts-with CSS:
div[id^='abcd']
  • starts-with and contains xPath:
//div[starts-with(@id, 'abcd') and contains(@id,'selenium')]

If element is something like this then you can use multiple contains

HTML:

<div id='12abcd$selenium450'>
  • contains and contains xPath:
//div[contains(@id, 'abcd') and contains(@id,'selenium')]
  • Related