简体   繁体   中英

Selenium/Python2.7 Find elements with similar src?

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!

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM