Skip to content
Advertisement

How to extract the values of the alt atributte within img with selenium python

I have the following problem, I haven’t been able to solve it. I have to extract the text that appears in an alt attribute of an image. The id always changes, as does the contained alt.

Example

I noticed that the id only changes in this part

//*[@id="j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:**2**:j_id1102997597_32d04d7a:**2**:j_id1102997597_32d04d8e:j_id415163359_4cfbfc60"]
//*[@id="j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:**1**:j_id1102997597_32d04d7a:**1**:j_id1102997597_32d04d8e:j_id415163359_4cfbfc60"]
//*[@id="j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:**0**:j_id1102997597_32d04d7a:**0**:j_id1102997597_32d04d8e:j_id415163359_4cfbfc60"]

Anyway, I am still unable to log in.

Answer

You can use BeautifulSoup for this (install with pip install bs4):

from bs4 import BeautifulSoup

soup = BeautifulSoup(browser.page_source, 'html.parser')

images = soup.select_one('div.text-center').select('img')

for image in images:
print(image.get('alt'))
Advertisement