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
.
I noticed that the id
only changes in this part
JavaScript
x
4
1
//*[@id="j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:**2**:j_id1102997597_32d04d7a:**2**:j_id1102997597_32d04d8e:j_id415163359_4cfbfc60"]
2
//*[@id="j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:**1**:j_id1102997597_32d04d7a:**1**:j_id1102997597_32d04d8e:j_id415163359_4cfbfc60"]
3
//*[@id="j_id1102997597_32d04ef7:0:j_id1102997597_32d04d36:**0**:j_id1102997597_32d04d7a:**0**:j_id1102997597_32d04d8e:j_id415163359_4cfbfc60"]
4
Anyway, I am still unable to log in.
Advertisement
Answer
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:
JavaScript121print([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]")))])
2
Using XPATH:
JavaScript121print([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]")))])
2
Note: You have to add the following imports :
JavaScript141from selenium.webdriver.support.ui import WebDriverWait
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support import expected_conditions as EC
4