0

What would be the Xpath to get the background-image CSS Property of a DIV tag whose ID is mentioned in Selenium Webdriver?

Example:

<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

1
  • Not sure what you're trying to do here. Do you know the div's ID, and you want to verify the CSS background-image property? Or do you only know the CSS background-image property, and you want to use that to identify the div and then do some other testing on it? Sep 17 '13 at 10:19
2

Typically HTML IDs are unique; if you know the ID, you do not need to know the element tag. Of course it is possible to write a web page that uses duplicate IDs. If your web page has duplicate IDs, you should complain to the developer. For the rest of my answer, I will assume unique IDs.

According to this, XPath doesn't actually know anything about CSS styles.

I also assume you are using the Java language binding. I Googled for "webdriver webelement"; the second link took me to the WebElement javadoc, where I found the getCssValue method. Try this:

WebElement element = driver.findElement(By.id("abc"));
String bgImage = element.getCssValue("background-image");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.