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

Time:07-22

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.

CodePudding user response:

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'))

CodePudding user response:

To extract and print the values of the alt attribute you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("alt") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.matriz table > tbody tr.filaMatriz > td > img[alt]")))])
    
  • Using XPATH:

    print([my_elem.get_attribute("alt") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='matriz']//table/tbody//tr[@class='filaMatriz']/td/img[@alt]")))])
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related