I'm trying to scrape some image href attributes from the <a>
tags from a Google search using Selenium, ChromeDriver and Chrome. When the url is accessed manually I'm able to locate the required href
attributes but when I initiate a Google Chrome session through ChromeDriver those href
attributes aren't available in the DOM and I get empty results.
Code trials:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
keyword = "cats"
url = "https://www.google.com/search?q=" + keyword + "&source=lnms&tbm=isch&tbs="
print(url)
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get(url)
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.islrc>div>a")))])
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='islrc']/div/a")))])
HTML of manually launched Chrome observed through Inspect:
<a class="wXeWr islib nfEiy mM5pbd" jsname="sTFXNd" jsaction="click:J9iaEb;" data-nav="1" tabindex="0" style="height: 180px;" href="/imgres?imgurl=https%3A%2F%2Ficatcare.org%2Fapp%2Fuploads%2F2018%2F07%2FThinking-of-getting-a-cat.png&imgrefurl=https%3A%2F%2Ficatcare.org%2Fadvice%2Fthinking-of-getting-a-cat%2F&tbnid=0V922RrJgQc9SM&vet=12ahUKEwi8rsyAvKrqAhVPBysKHfuyCwgQMygAegUIARDPAQ..i&docid=5qEHfJOysK_DwM&w=1200&h=600&q=cats&ved=2ahUKEwi8rsyAvKrqAhVPBysKHfuyCwgQMygAegUIARDPAQ" data-navigation="server">
Snapshot:
HTML of Selenium launched Chrome:
<a class="wXeWr islib nfEiy mM5pbd" jsname="sTFXNd" jsaction="click:J9iaEb;" data-nav="1" tabindex="0" style="height: 180px;"><div class="bRMDJf islir" jsname="DeysSe" style="margin-top: 11px; margin-left: 19px; margin-right: 18px;" jsaction="mousedown:npT2md; touchstart:npT2md;">
Snapshot:
I'm using the Selenium Python client. Can someone help me to understand the difference?