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>
1

4 Answers 4

8

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:

Sign up to request clarification or add additional context in comments.

5 Comments

That was helpful but I am getting the value as "url( "test.com/images/image.png") " how to get that exact link which is inside the url
@Anjali Checkout the updated answer and let me know the status.
@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 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.
@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?
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"))

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.