MOs
MOs

Reputation: 27

How do I get the background url from css Selenium Webdriver

I'm trying to check the url of an image, located in a div - in the 'background:' - in the CSS for the page. But when 'get' the CssValue and display it just shows blank.

The html for the div is:

<div class="Welcome">

<h3>Welcome</h3>
.......
</div>

...and the CSS for the div is:

div.Welcome {
background: rgba(0, 0, 0, 0) url("imageurl") no-repeat scroll center 25px;
float: left;
margin: 50px 20px 50px 150px;
padding-top: 230px;
width: 300px;
}

I've tried to locate as below:

WebElement WelcomeImg = driver.findElement(By className("Welcome"));

String WelImg = WelcomeImg.getCssValue("background");

Assert.assertTrue(WelImg.contains("imageurl"));

I've also tried with

("background-url");

Any help would be greatly appreciated

Upvotes: 1

Views: 2035

Answers (1)

optimistic_creeper
optimistic_creeper

Reputation: 2799

I think, you should try with new thing! Get the background image property and let me know whether it worked.

WebElement WelcomeImg = driver.findElement(By className("Welcome"));
String WelImg = WelcomeImg.getCssValue("background-image");
Assert.assertTrue(WelImg.contains("imageurl"));

Hope, it may help you.

Upvotes: 2

Related Questions