-1

So I'm beginner at selenium again....

I have a problem of getting the images in a web page here ANIMEPAGE So basically here's the console I want to get_attribute in this image enter image description here enter image description here

As you see I'm trying to get the style background image but I can't get it with these lines of code that I've search.

containers = driver.find_elements(by="xpath",value='//div[@class="herald box news"]')

news_link_init = []
news_intro_init = []
news_full_init = []
news_image_init = []

for container in containers:


    news_link = container.find_element(by="xpath", value="./div[@class='wrap']//h3/a").get_attribute("href")
    news_intro = container.find_element(by="xpath", value="./div[@class='wrap']//span[@class='intro']").text
    news_full = container.find_element(by="xpath", value="./div[@class='wrap']//span[@class='full']").text

// AS YOU SEE HERE I"M TRYING TO GET THE ATTRIBUTE STYLE BUT I CAN"T GET IT AND IT GIVES ME ERROR HERE
    news_image = container.find_element(by="xpath", value="./div[@class='thumbnail']").get_attribute("style")

    news_link_init.append(news_link)
    news_intro_init.append(news_intro)
    news_full_init.append(news_full)

    // Append images
    news_image_init.append(news_image)

As you see here in the code I'm trying to get those background-image:url() but I can't..can anyone help me on these ? Thanks.

Update: This is the always error I have receive based on two answers comment down below

enter image description here

Myth Vince
  • 143
  • 9

1 Answers1

1

You can try value_of_css_property method and with a little bit of cleanup.

The code:

# getting information from the image using `value_of_css_property method
news_image = container.find_element(by="xpath", value="./div[@class='thumbnail']").value_of_css_property('background-image')

## Output: 'url("https://cdn.animenewsnetwork.com/thumbnails/cover400x200/youtube/NKQYx6mGQPE.jpg")'

# from the get the core URL, as the value contains *url* declaration with a combination of `lstrip` and `rstrip`
news_image = news_image.lstrip('url("').rstrip('")')

## Output: https://cdn.animenewsnetwork.com/thumbnails/cover400x200/youtube/NKQYx6mGQPE.jpg

I haven't tested the code yet, but if it doesn't work, let me know :)


Update

Images on the site loads on scroll. This is the result prior to scrolling.

enter image description here

This after I have manually scrolled to the end:

enter image description here

You need to go to the bottom of the page, then wait a bit for the images to load. Here is another post relevant to this:

How to scroll down the page till bottom(end page) in the Selenium WebDriver

Reincoder
  • 111
  • 2
  • 7