2
from selenium import webdriver


fp = webdriver.FirefoxProfile('')
driver = webdriver.Firefox(firefox_profile=fp)
driver.set_window_size(1400, 1000)
driver.get('')


list_of_elements = driver.find_elements_by_css_selector('img[title]')
for ele in list_of_elements:
    print ele

I'm trying to print out all the img src=' ' of images on a webpage, the above code is for css selector and works well but I would like to do the same thing but have the code print out the src links of the images. Basically what im trying to do is make a list of all elements on the webpage based on the src= and then I can search through the src= and find the one I want. Any help is appreciated thanks!

1 Answer 1

1

Looks like you are interested in get_attribute():

srcs = [ele.get_attribute("src") for ele in list_of_elements]

Note that, since you are going to filter out elements based on src later - depending on how complicated the check would be, you may solve it by a single CSS selector. For instance, let's say you want to locate img element having the title attribute and "test" inside the src attribute:

driver.find_elements_by_css_selector('img[title][src*=test]')

Here *= means "contains".

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

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.