Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = ?????
like image 431
spooky123 Avatar asked Jan 26 '26 17:01

spooky123


1 Answers

Use GetAttribute() method:

var element = driver.FindElement(By.TagName("img"));
string imageSrc = element.GetAttribute("src")
like image 55
alecxe Avatar answered Jan 28 '26 09:01

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!