1

I have tried to scrape whatsapp. i just have to scrape all the names and numbers(also not save people's contact) present in my chat list.

Try these in .ipynb file to login to the whatsapp My code :

1)
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import sys
import pyautogui
import datetime
import re
from time import *
from selenium.common.exceptions import StaleElementReferenceException

2)
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com/")
sleep(13)
print("Chrome has been automated",eta(time()))

3)
driver.maximize_window()
mt_contacts = set()
contact_dict = {}
i=1
while True:
    contacts = driver.find_elements(By.CSS_SELECTOR, '._8nE1Y')
    # print([i.text for i in contacts])
    for element in contacts[2:]:
        try:
            element.click()
            sleep(0.5)
            try:
                driver.find_element(By.CLASS_NAME, '_24-Ff').click()
                sleep(0.5)
                try:
                    name = [i.text for i in driver.find_elements(By.CLASS_NAME,'q9lllk4z')]
                    if len(name)>0:
                        name=name[0]
                        number = [i.text for i in driver.find_elements(By.CLASS_NAME,'qt60bha0')]
                        if len(number)>0:
                            number=number[0]
                            print(name,number)
                            mt_contacts.update([i.text for i in driver.find_elements(By.CLASS_NAME,'q9lllk4z')])
                            contact_dict[name]=number
                except StaleElementReferenceException as e:
                    print(element.text)
                    pass
            except StaleElementReferenceException as e:
                print(element.text)
                pass
        except StaleElementReferenceException as e:
            print(element.text)
            pass
    sleep(5)
    driver.execute_script(f"window.scrollTo(0, {i*1000})")     
    i+=1
    if i > 10:
        break

i have to open every contact and then click on their profile to see their number and name . But when i opens the last element from the current page, it scrolls down the page but if the element was present at the top and is unvisible it is unclickable.

i have two options: one is to scroll up and search for the element and another one is to skip that particular element. But when i tried to skip that element it showed an error instead there was try and except block.

Try and except block is not working for StaleElementReferenceException

error :

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
...
#18 0x559fe3d80998 
#19 0x559fe3d80b27 
#20 0x559fe3d9bc23 
#21 0x7f3f2a45e609 start_thread

0 Answers0