0

I'm trying to read some messages in Discord with selenium, but there a specific one that the webdriver seems unable to read.

chat_messages = browser.find_elements(By.CLASS_NAME, "markup-eYLPri")
for chat_message in chat_messages:
    print(chat_message.get_property("textContent"))

I have tried with:

element.get_property("textContent")
element.get_property("innerText")
element.text

All of them return an empty string.

However, in the browser, I've tried running this javascript snippet:

e = document.getElementById("message-content-1008775068500893826")
e.innerText
e.textContent

And, they did print the text as expected.

Is there anything I am missing?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

The second part of the classname i.e. eYLPri is dynamically generated and is bound to change sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.


Solution

To print textContent you can use the get_attribute() method and of the element you can use either of the following locator strategies:

  • Using css_selector:

    chat_messages = browser.find_elements(By.CSS_SELECTOR, "[class^='markup']")
    for chat_message in chat_messages:
        print(chat_message.get_property("textContent"))
    
  • Using xpath:

    chat_messages = browser.find_elements(By.XPATH, "//*[starts-with(@class, 'markup')]")
    for chat_message in chat_messages:
            print(chat_message.get_property("textContent"))
    

Update

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("textContent") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[class^='markup']")))])
    
  • Using XPATH:

    print([my_elem.get_attribute("textContent") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[starts-with(@class, 'markup')]")))])
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352