1

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.

9
  • //*[contains(@id,'')] then use .get_attribute('alt') Commented Jul 21, 2022 at 17:07
  • Hi @ArundeepChohan it is correct ? Imagen = driver.find_elements(By.XPATH,"//*contains(@id,'j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:')]").get_attribute('alt') Commented Jul 21, 2022 at 17:27
  • can you share url of the page? Commented Jul 21, 2022 at 17:49
  • Yes, but it is in spanish santander.cl/cotizador-web Commented Jul 21, 2022 at 17:52
  • in that case, can you post screenshot of the element you want to extract Commented Jul 21, 2022 at 17:55

2 Answers 2

1

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
    
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.