Home > database >  Selenium: How to extract the CSS background-image value
Selenium: How to extract the CSS background-image value

Time:04-02

HTML:

<div  style="background-image: url(&quot;/img/all/05_element/continentImg/1.png&quot;);"></div>

at this html part how can I locate the background-image?

CodePudding user response:

To get the background image you need to use value_of_css_property(property_name) method and inducing 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, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class='__section-image']"))).value_of_css_property("background-image")
    print(re.split('[()]',my_property)[1])
    
  • Using XPATH:

    import re
    
    my_property = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='__section-image']"))).value_of_css_property("background-image")
    print(re.split('[()]',my_property)[1])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

CodePudding user response:

can i click that image ? and can i locate that?

  • Related