Skip to content
Advertisement

How to get text from a html using Selenium and Python which has two elements with the same classname where I need to extract both

I have a html like:

<div class='mesage-in'> cool text here </div>
<div class='mesage-in'> bad text here </div>

and my python code like:

texto = navegador.find_element_by_class_name('message-in').text
print(texto)

Is it possible make this get all elements with same class name and put on a array or define as different variable like this?

OutPut:

print(texto1)

-> cool text here

print(texto2)

-> bad text here

#or

print(texto[0])

-> cool text here

print(texto[1])

-> bad text here

Actualy my code only get the first one

Advertisement

Answer

As per the HTML:

<div class='mesage-in'> cool text here </div>
<div class='mesage-in'> bad text here </div>

The following line line of code:

texto = navegador.find_element_by_class_name('message-in').text

will always identify the first matching element, extract the text and assign it to texto. So when you try to print texto, the text of the very first element i.e. cool text here is printed.


Solution

You can get all elements with same classname i.e. mesage-in and put on a list as follows:

from selenium.webdriver.common.by import By
texto = navegador.find_elements(By.CLASS_NAME, 'message-in')

Now you can print the desired texts with respect to their index as follows:

  • To print cool text here:

    print(texto[0].text) # prints-> cool text here
    
  • To print bad text here:

    print(texto[1].text) # prints-> bad text here
    

Outro

You can also crate a list of the texts using List Comprehension and print them as follows:

texto = [my_elem.text for my_elem in driver.find_elements(By.CLASS_NAME, "message-in")]
print(texto[0]) # prints-> cool text here
print(texto[1]) # prints-> bad text here
Advertisement