Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath to get Background-image attribute in DIV

What can be the Xpath to get the background-image CSS Property of a DIV tag whose ID is mentioned in (selenium webdriver)?

Ex: (div id="abc", style="width: 538px !important; height: 242px !important; background-image: url(http://test.com/images/abc.png); position: relative; background-position: 0% 0%;")

I want to find this image with url:(http://test.com/images/abc.png)

like image 694
Bhakti Shah Avatar asked Dec 09 '22 12:12

Bhakti Shah


1 Answers

I don't get your question, please format and expand a bit, it's kind of vague to me.

I assume you want to ask one of the following questions, but not sure which.

  • How to get the background-image CSS Property of a div using Selenium?

Answer: Use Selenium's native GetCssValue() (C#), css_value (Ruby), or equivalent method in other language bindings.

IWebElement abc = driver.FindElement(By.XPath("//[@id='abc']")); // use XPath as you requested
string imageUrl = abc.GetCssValue("background-image");
  • How to locate a div element by its background-image CSS property using XPath?

Answer: If you don't want to use ID (which you should in this example case), you can totally do it in CssSelector or XPath. (XPath is the last option you should choose though)

IWebElement abc = driver.FindElement(By.XPath("//div[contains(@style, 'background-image: url(http://test.com/images/abc.png);')]"));
  • How to use XPath to get an element's attribute content which has background-image?

Answer: Not really useful for Selenium, but here you have it: //div[@id='abc']/@style

like image 140
Yi Zeng Avatar answered Dec 22 '22 02:12

Yi Zeng