Ponsukumar lingam
Ponsukumar lingam

Reputation: 13

How to locate this HTML tag through Selenium?

HTML:

<img src="dash_img/dash-img_07.png">

HTML Image:

enter image description here

Here there is no attribute to get the xpath, I tried by link text but that too failed.

Upvotes: 0

Views: 127

Answers (4)

JeffC
JeffC

Reputation: 25714

I think the most straightforward way to find it would be to use a CSS selector

img[src='dash_img/dash-ig_07.png']

or XPath

//img[@src='dash_img/dash-ig_07.png']

CSS selectors are faster, better supported, and a simpler syntax to learn but there are times when you have to use XPath, e.g. when you want to find an element by contained text.

But... I would suggest that you find it based on the image label, "View Timesheet",

//span[.='View Timesheet ']/preceding::img[1]

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193338

To locate the element you can use either of the following solutions:

  • CssSelector:

    div.Timesheet li.time-bg > a > img[src='dash_img/dash-img_07.png']
    
  • XPath:

    //div[@class='Timesheet']//li[@class='time-bg']/a/img[@src='dash_img/dash-img_07.png']
    
  • XPath (based on the text View Timesheet):

    //span[contains(., 'View Timesheet')]//preceding::a[1]/img
    

Upvotes: 1

Mate Mrše
Mate Mrše

Reputation: 8444

Or

"//div[@class='list']//a/img"

Upvotes: 0

IMParasharG
IMParasharG

Reputation: 1905

You can use this xpath should find it

WebElement temp = driver.findElement(By.xpath("//img[contains(@src,'<img src="dash_img/dash-img_07.png">')]"));

Upvotes: 0

Related Questions