8

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)
1
  • You shouldn't use XPath in this case: //a[@class='poster ']. In terms of HTML, this could just as well be <a class="poster">, <a class=" poster"> or <a class=" poster ">. Better use a CSS selector: a.poster Commented Apr 1, 2016 at 13:24

1 Answer 1

19

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