1

I'm trying to get a background image url using xpath .

my xpath expression so far:

//div[@class='Header']/div[@class='Header-jpeg']

which points to the div that has the background-image but from there no idea how to get the img url, probably something in @style but no luck so far.

Css of that div:

div.Header-jpeg {
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    width: 874px;
    height: 94px;
    background-image: url('images/Header.jpg');
    background-repeat: no-repeat;
    background-position: center center;
}

and a real example ca be found here: http://www.landenkompas.nl/

2 Answers 2

2

Something like this should work:

substring-before(substring-after(//div[@class='Header']/div[@class='Header-jpeg']/@style, 'background-image: url('), ')')

However, be warned that if you're using Java XPath it will only get the first element it finds. If you have multiple, you'll need to find another way to aggregate all the values.

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

1 Comment

That's the right answer as far as I remember, it might get tricky though sometimes as some sites use something slightly different like " vs i or an extra space etc
0

Assuming you are using selenium, you need to get a value of the CSS property.

In Java:

WebElement element = driver.findElement(By.xpath("//div[@class='Header']/div[@class='Header-jpeg']"));
element.getCssValue("background-image");

In Python:

element = driver.find_element_by_xpath("//div[@class='Header']/div[@class='Header-jpeg']")
element.value_of_css_property("background-image")

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.