C# Selenium - Can't access image url in another tag

Viewed 84

I'm trying to validate image on website however can't access the url of the image. Most websites will have the image with a "src" tag however this website has the url imbedded in another tag.

Iv used return driver.FindElement(By.ClassName("p-image__image--contain")); to access the element and used GetAttribute("style"); to access the image url. However the only information given was background-position: center center;

html format

2 Answers

When you use GetAttribute("style"), it will only return whatever was in the actual style attribute on the element, but many CSS attributes are actually applied using class stylesheets or other ways. So instead you should use the GetCssValue method, like so:

element.GetCssValue("background-image")

getCssValue(); will help in this case

WebElement img = driver.findElement(By.className('imgClass'));
String imgPath = img.getCssValue("background-image");

you will got full values in imgPath, Value like url("imageURL") then you need to use regex OR get values by slip function

I hope this scenario will help

Related