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

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



Solution 1:[1]

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")

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MatthewG
Solution 2 Kishan Vaishnani