Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fetch `style="background-image: url` using selenium webDrive?

I grabbed this element using selenium webDrive:

<div class="body" style="background-image: url('http://d1oiazdc2hzjcz.cloudfront.net/promotions/precious/2x/p_619_o_6042_precious_image_1419849753.png');">

how can I fetch the value: http://d1oiazdc2hzjcz.cloudfront.net/promotions/precious/2x/p_619_o_6042_precious_image_1419849753.png ?

I'm not sure as this is an inner value, and not just an "src" attribute.

like image 225
Elad Benda2 Avatar asked Nov 14 '25 18:11

Elad Benda2


2 Answers

getCssValue(); will help you

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

then you can split the unwanted string "url('"

PS : Remove the javascript tag in your question

like image 192
ra_sangeeth Avatar answered Nov 17 '25 06:11

ra_sangeeth


Try this

var imgString = $(".body").css('background-image');
console.log (imgString.split("(")[1] // remove open bracket
                      .split(")")[0] // remove close bracket
             );

Fiddle

like image 31
Sudharsan S Avatar answered Nov 17 '25 08:11

Sudharsan S



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!