0

Html code Span is a button X which is present top right side of the image

1st Page
<img src="img.jpg" class="class2">

<span>                                                  how to interact with span
   <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg">
   <path d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 "></path></svg>
</span>                                                   how to interact with span
  • If you don't have any class or id to identify which span you want to interact with the only way I'd see would be its position within the document (e.g. the 1st span after the img with class "class2", the nth span in the document etc.). Structural changes to the document could break the test though. – Thomas Nov 08 '21 at 14:08
  • Can you remove the 2 instances of the phrase _how to interact with span_ if they are originally not a part of the HTML? – undetected Selenium Nov 08 '21 at 14:42

2 Answers2

2

The element you are trying to find is within a <svg> tag so regular locators won't work. You need to try like below,

//*[name()='svg' and @stroke='currentColor']

or

//span//*[name()='svg' and @stroke='currentColor']

or

//*[name()='svg' and @stroke='currentColor']//*[name()='path']
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Nandan A
  • 2,702
  • 1
  • 12
  • 23
0

If the span contains something unique, select the span that contains that. For example the svg:

"//span[./svg[@viewBox='0 0 512 512']]"

Or, if the your entire code is surrounded by a div, as shown below. Get the div which has the right image and then grab the span INSIDE the div:

<div>
    <img src="img.jpg" class="class2">
    <span>
         ....
    </span>
</div>

"//div[./img[src='img.jpg']]/span"

See also finding child elements

Taco Verhagen
  • 222
  • 1
  • 6
  • You are 100% right. But If image names are Dynamic then? – Saikumar Reddy Yelampalli Nov 08 '21 at 14:27
  • @Saikumar well, find anything that is unique on the page and build from there. Maybe the image is inside a table structure where you can identify the cell. Or if you want to get them all, use a findElements. If you simply want the second "(//div[./img]]/span)[1]" – Taco Verhagen Nov 08 '21 at 15:31