4

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.

ra_sangeeth
  • 467
  • 3
  • 13
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

3 Answers3

7

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

ra_sangeeth
  • 467
  • 3
  • 13
  • how can I fetch with regex the url itself? `url('http://d1oiazdc2hzjcz.cloudfront.net/promotions/precious/2x/p_619_o_6042_precious_image_1419849753.png');">` – Elad Benda2 Dec 29 '14 at 13:35
  • http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/ this may help you! – ra_sangeeth Dec 29 '14 at 13:41
4

Try this

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

Fiddle

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
0

You can try the following

some_variable=self.driver.find_element_by_xpath("//div[@class='body' and contains(@style,'url')]")
some_variable2=some_variable.get_attribute('style')
Saurav Rai
  • 2,171
  • 1
  • 15
  • 29