spooky123
spooky123

Reputation: 153

How do I get the img tag "src" value from a Selenium Web Browser element as a string?

I'm new to Selenium and i'm trying to get the "src" value from a IWebElement and convert it to a string (C#). I am able to get a simple text value from elements using their built in .Text, but that will only give me the inner html. Here's the sample code:

IWebDriver driver = new FirefoxDriver();            
driver.Navigate().GoToUrl("http://somewebsite");        
var val = driver.FindElement(By.TagName("img"));
string imageSrc = ?????

Upvotes: 3

Views: 8644

Answers (2)

Vitali Volkov
Vitali Volkov

Reputation: 1

use xPath, for example

//img[@src='The image src']

Also you can use assert.istru and .Displayed

Upvotes: 0

alecxe
alecxe

Reputation: 473833

Use GetAttribute() method:

var element = driver.FindElement(By.TagName("img"));
string imageSrc = element.GetAttribute("src")

Upvotes: 8

Related Questions