vladik3333,
<span>текст1</span>
<span>текст1</span>
<span>текст2</span>
<span>текст1</span>
<span>текст2</span>
<span>текст1</span>
<script>
let str = 'текст2';
let index = [...document.querySelectorAll('span')].findIndex(({textContent}) => textContent.includes(str));
alert(index);
</script>