Issue
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
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
Solution
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.
This after I have manually scrolled to the end:
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
Answered By - Reincoder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.