0

I tried to collect some text with selenium webdriver using python and I found that something.text collect empty string, but something.get_attribute(name="innerText") collect value I want. Why is this happening ?

something = driver.find_element(By.CLASS_NAME, "block-mega-child-cats")
tag = something.find_elements(By.TAG_NAME, "a")
for i in tag:
    print(i.get_attribute(name="innerText"))

This part of code works perfectly, but I can't understand why .text doesn't.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Zvezda Bre
  • 61
  • 4

1 Answers1

0

To collect all the texts you need to induce WebDriverWait for visibility_of_all_elements_located() and using List Comprehension you can use either of the following solution:

  • Using CSS_SELECTOR and get_attribute("innerHTML"):

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".block-mega-child-cats a")))])
    
  • Using XPATH and text attribute:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@class='block-mega-child-cats']//a")))])
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352