0

if this is the html of a whatsapp message ("😅 how 👹 are you 🎂") then how to iterate through elements of this message and get them (print them) in order as they are by selenium?

   <span dir="ltr" class="i0jNr selectable-text copyable-text">
    <span>
        <img crossorigin="anonymous"
            src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="😅"
            draggable="false" class="b75 emoji wa i0jNr selectable-text copyable-text" data-plain-text="😅"
            style="background-position: -60px -40px;">
        " how "
        <img crossorigin="anonymous"
            src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="👹"
            draggable="false" class="b60 emoji wa i0jNr selectable-text copyable-text" data-plain-text="👹"
            style="background-position: -60px -40px;">
        " are you"
        <img crossorigin="anonymous"
            src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="🎂"
            draggable="false" class="b25 emoji wa i0jNr selectable-text copyable-text" data-plain-text="🎂"
            style="background-position: -40px -40px;">
    </span>
</span>

output this should be

😅
 how
👹
 are you
🎂

or output can also be like this

😅 how 👹 are you 🎂

i tried this

chats = driver.find_elements_by_class_name("message-in")
for i in range(0,len(chats)):
    messages = chats[i].find_elements_by_class_name("i0jNr")
    for j in range(0,len(messages)):
        if messages[j].text == "" :        
            emojis = chats[i].find_elements_by_class_name("emoji")
            for emoji in emojis:
                print(emoji.get_attribute('alt'))
                break
        else:
            print(messages[j].text)

this is giving output as

 how
 are you
😅
👹
🎂 

so how to get elements of this in order as they are ?

1 Answer 1

1

You can iterate over the child of span element and print the text in case of string and alt text in case of img tag

from bs4 import BeautifulSoup as bs4
from bs4 import NavigableString, Tag

soup = bs4(html, 'html.parser')

s = soup.find('span', attrs={'class':'i0jNr'})
s = s.find('span')
for i in s.children:
    if isinstance(i, NavigableString):
        print(i.strip())
    elif isinstance(i, Tag):
        print(i.attrs['alt'])

here is code sample for your use case It's output is for this message is

😅
how
👹
are you
🎂
Sign up to request clarification or add additional context in comments.

5 Comments

actually i dont know bs4 much ,how to combine selenium and bs4 ? .this is whole code from bs4 import BeautifulSoup as bs4 from bs4 import NavigableString, Tag from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(r'C:\Users\PRANAV PATIL\Downloads\chromedriver.exe') driver.get(r'https://web.whatsapp.com/') input("enter any key :") searchbox = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//div[@id='side']//div//div//label//div//div[@contenteditable='true']"))) searchbox.send_keys('diksha') # enter your sender's name searchbox.send_keys(Keys.RETURN) input("enter any key :")
soup = bs4(html, 'html.parser') s = soup.find('span', attrs={'class':'i0jNr'}) s = s.find('span') for i in s.children: if isinstance(i, NavigableString): print(i.strip()) elif isinstance(i, Tag): print(i.attrs['alt'])
this is the whole code @Gr8ayu but its giving error at this place ,actually i dont know bs4 its giving error at html word soup = bs4(html, 'html.parser') what to do for that html word? error is html is not defined @Gr8ayu
you need to initialize html with your html code, you can use like html = driver.page_source

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.