Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get img from selenium

how to get the image hyperlink by using the selenium package.

from selenium import webdriver 
driver = webdriver.Firefox()
driver.get("http://m.imdb.com/feature/bornondate")
elements = driver.find_elements_by_xpath("//a[@class='poster ']")
li = [["Name","Movie Title","Image"]]
for i in  elements:
    print i.find_element_by_tag_name("img") ##I am not sure how to get the URL
    new_line= i.text.splitlines()
    #print new_line[0] , " " , new_line[1]
    li.append(new_line)

print li 

Writing the data to CSV file

with open ('imdb.csv','wb')as fp:
    a = csv.writer(fp, delimiter=',')
    a.writerows(li)
like image 730
SQLGuru Avatar asked Apr 01 '16 11:04

SQLGuru


1 Answers

To get a element property like src you need to call the get_attribute('attr_name') property.

You just need to add the following code to your for cycle:

for i in  elements:
    image = i.find_element_by_tag_name("img")
    img_src = image.get_attribute("src")
like image 105
Gil Sousa Avatar answered Sep 19 '22 23:09

Gil Sousa