Im building a webscrape bot and I want to get the alt text of images from instagram posts. In theory this should not be that hard with .get_attribute('alt') command in Selenium but for some reason this never works. It always returns none.
Here's the code of my whole function:
def get_image_alt_text(posts):
alt_text = []
for link in posts:
driver.get(link)
time.sleep(4)
try:
image = driver.find_element(by=By.TAG_NAME, value="img")
alt_text.append(image.get_attribute('alt'))
print(alt_text)
except:
continue
return alt_text
when I run this it just puts an empty string into the list. I also realize this isn't great code as there are usually multiple image tags on a page. So I rewrote it to find_elements() and then iterated every image on the page and none returned any alt text. Even though when inspecting the HTML on the site there is a value set for alt.
CodePudding user response:
I ended up getting around this issue by using .get_attribute('innerHTML') and then making a separate function to slice out the alt value from the given string.