4

Using python how can I get the background image using selenium? Which has an inline CSS? I want to get the image URL from the background-image: url()

<div id="pic" class="pic" data-type="image" style=" background-image: url(http://test.com/images/image.png;); height: 306px; background-size: cover;"></div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Anjali
  • 187
  • 1
  • 4
  • 12
  • FYI! https://stackoverflow.com/questions/42187922/selenium3-python3-how-to-get-url-from-attribute-style-background-image-ur – Umair Latif Nov 29 '19 at 10:37

4 Answers4

7

To get the background image you need to use value_of_css_property(property_name) method and you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    import re
    
    my_property = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pic#pic[data-type='image']"))).value_of_css_property("background-image")
    print(re.split('[()]',my_property)[1])
    
  • Using XPATH:

    import re
    
    my_property = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='pic' and @id='pic'][@data-type='image']"))).value_of_css_property("background-image")
    print(re.split('[()]',my_property)[1])
    
  • Console Output:

    test.com/images/image.png
    

Update

As the url is getting wrapped up with in double quotes i.e. "..." you can use the following solution:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='pic' and @id='pic'][@data-type='image']"))).value_of_css_property("background-image").split('"')[1])

References

You can find a couple relevant discussions related to:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    That was helpful but I am getting the value as "url( "http://test.com/images/image.png") " how to get that exact link which is inside the url – Anjali Nov 29 '19 at 11:45
  • 1
    @Anjali Checkout the updated answer and let me know the status. – undetected Selenium Nov 29 '19 at 12:02
  • 1
    @DebanjanB your approach gave the result with " " at both ends but adding [ " ] this pattern to the result gave me the exact URL. Thank you for your solution – Anjali Nov 29 '19 at 13:12
  • @Anjali Going by the HTML the _css_ property is `background-image: url(http://test.com/images/image.png;);` and have no quotes around. So the webapp may be adding the quotes to the _string_ which we can't recognize unless we run the apps. I understand adding **`"`** within the pattern solved your issue. – undetected Selenium Nov 29 '19 at 13:17
  • @Anjali Can you check and update me the result using the alternative option I have added as an update to the answer if that works? – undetected Selenium Dec 05 '19 at 12:02
1

Try This:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pic#pic[data-type='image']"))).get_attribute("href"))
Hamza Lachi
  • 1,046
  • 7
  • 25
0

To get the URL You need to use regular expression. In order to use regular expression Import re and then use the following regular expression.

import re
itemimage=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#pic"))).value_of_css_property("background-image")
print(re.search("(?P<url>http?://[^\s;]+)", itemimage).group("url")) 

Output:

http://test.com/images/image.png
KunduK
  • 32,888
  • 5
  • 17
  • 41
0
 my_setup = WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH, "//body/div[@class='container']/div[@class='row']/div[@class='card-stack']/div/div[1]/div[1]"))).value_of_css_property("background-image")

image_url = re.split('[()]',my_setup)[1]

use this actually its working fine for me..

main things..

.value_of_css_property("background-image")  

not working for a list

itronic1990
  • 1,231
  • 2
  • 4
  • 18