Nick Kahn
Nick Kahn

Reputation: 20078

How to verify if the image in Selenium using Webdriver

How would I verify the image is displaying is the correct path/name in Selenium using WebDriver?

I started using this code but not sure :

string _active = "<img style="display: ;" alt="Active" src="../App_Themes/Default/images/check.png"/>";
driver.FindElement(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);

Upvotes: 1

Views: 16876

Answers (1)

Hari Reddy
Hari Reddy

Reputation: 3858

I would like to clarify to you that this code

driver.FindElement(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);

does not give you the html code for the image tag but a IWebElement object. And you can read the various attributes of this WebElement by using the GetAttribute method.

You will have to get the src attribute of the img tag you are looking for by locating the image(webelement) by xpath and then

IWebElement element = driver.FindElement(By.XPath("Your xpath"));
        string path = element.GetAttribute("src");

Now you can verify the path of your image. Hope this helps you.

Upvotes: 3

Related Questions