Home > Back-end >  how to interact with span in selenium java without class or id?
how to interact with span in selenium java without class or id?

Time:11-09

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

CodePudding user response:

The element you are trying to find is having 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']

CodePudding user response:

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

  • Related