0

I'm trying to scrape images from Google images . My code:

from selenium import webdriver
import time

keyword = "cats"
url = "https://www.google.com/search?q=" + keyword + "&source=lnms&tbm=isch&tbs="

driver= webdriver.Chrome()
driver.get(url)
time.sleep(5)

tags = driver.find_elements_by_xpath('//div[@id="islrg"]/div[@class="islrc"]/div/a[1]')

for tag in tags:
    link = tag.get_attribute("href")
    print(link)

This will print only None.

In the page source the tags have no href , I can see it only from inspect element . How can I get the href of these elements?

1 Answer 1

1

Cause of error;

The href you want needs to be loaded

from selenium import webdriver
from time import sleep

keyword = "cats"
url = "https://www.google.com/search?q=" + keyword + "&source=lnms&tbm=isch&tbs="

driver= webdriver.Chrome()
driver.get(url)

tags = driver.find_elements_by_xpath("//a[@class='wXeWr islib nfEiy mM5pbd']")

for tag in tags:
    tag.click()
    sleep(1)
    print(tag.get_attribute('href'))

solution;

I clicked on "tag" to install href

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

4 Comments

My xpath is correct , those are the elements I want . And they do have a href attribute but it's only visible from inspect element , not from page source . My question is how can I get it . Can selenium do this ?
Can you try the code and tell if you can get the output you want?
I don't want the hrefs from these elements . I want the hrefs from the elements in the OP .
what you wanted had to be loaded so it seemed to me that href was missing, I corrected the code you can try again

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.