Home > Blockchain >  I can't access the Add comment on instagram item. Python
I can't access the Add comment on instagram item. Python

Time:01-22

I'm trying to automate comments on an Instagram post using Python and Selenium. I am trying to access the text area where you write comments in all possible ways, such as CLASS_NAME, TAG_NAME, XPATH, CSS_SELECTOR. However, none of these methods are working. I have also tried using the implicit wait method to make sure the element is loaded, but that doesn't work either. I'm not sure what the problem is. I just need someone to tell me how to access the element where comments are written on an instagram post, send a send_keys to that element and hit the submit button.

The current status of the code is this:


    from selenium import webdriver
    import time
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import json
    import os
    import pickle
    from selenium import __version__
    
    
    data = {
    'username' : 'wendyturner329ysu',
    'password' : 'MpEgPAhd',
    'url_publication' : 'https://www.instagram.com/p/ClZj_e4vZCF/',
    'comment' : 'This is an automated comment!',
    'time' : 3
    }
    def Comments():
        driver = webdriver.Chrome()
        driver.get("https://www.instagram.com")
        cookies = pickle.load(open("cookies.pkl", "rb"))
        for cookie in cookies:
            if cookie['domain'] == ".instagram.com":
                try:
                    driver.add_cookie(cookie)
                except Exception as e:
                    print(e)
        driver.get("https://www.instagram.com/p/ClZj_e4vZCF/")
        time.sleep(4)
        driver.find_element(By.XPATH, '//textarea[@aria-label="Add a comment..."]').send_keys(data['comment'])
    
        time.sleep(5)
    Comments()

The error it throws is:

C:\Users\54225\AppData\Local\Programs\Python\Python310\python.exe C:/Users/54225/Documentos/Proyecto/Comentarios_Instagram_Selenium.py
Traceback (most recent call last):
  File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 39, in <module>
    Comentarios()
  File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 36, in Comentarios
    driver.find_element(By.XPATH, '//textarea[@aria-label="Agrega un comentario..."]').send_keys(datos['comentario'])
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 233, in send_keys
    self._execute(
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 410, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
    (No symbol) [0x00F36643]
    (No symbol) [0x00ECBE21]
    (No symbol) [0x00DCDA9D]
    (No symbol) [0x00DD09E4]
    (No symbol) [0x00DD08AD]
    (No symbol) [0x00DD0B30]
    (No symbol) [0x00DFC474]
    (No symbol) [0x00DFB7AB]
    (No symbol) [0x00E1FD7C]
    (No symbol) [0x00DF641F]
    (No symbol) [0x00E200D4]
    (No symbol) [0x00E36B09]
    (No symbol) [0x00E1FB76]
    (No symbol) [0x00DF49C1]
    (No symbol) [0x00DF5E5D]
    GetHandleVerifier [0x011AA142 2497106]
    GetHandleVerifier [0x011D85D3 2686691]
    GetHandleVerifier [0x011DBB9C 2700460]
    GetHandleVerifier [0x00FE3B10 635936]
    (No symbol) [0x00ED4A1F]
    (No symbol) [0x00EDA418]
    (No symbol) [0x00EDA505]
    (No symbol) [0x00EE508B]
    BaseThreadInitThunk [0x7612FEF9 25]
    RtlGetAppContainerNamedObjectPath [0x770A7BBE 286]
    RtlGetAppContainerNamedObjectPath [0x770A7B8E 238]


Process finished with exit code 1

When I use implicity wait this happens, this is the code:

datos = {
'usuario' : 'wendyturner329ysu',
'contraseña' : 'MpEgPAhd',
'url_publicacion' : 'https://www.instagram.com/p/ClZj_e4vZCF/',
'comentario' : 'Este es un comentario automatizado!',
'tiempo' : 3
}
def Comentarios():
    """Lo que esta comentado es para usar un proxy especifico"""
    #proxy = "136.228.220.49:8082"
    #options = webdriver.ChromeOptions()
    #options.add_argument('--proxy-server=%s' % proxy)
    #driver = webdriver.Chrome(chrome_options=options)
    driver = webdriver.Chrome()
    driver.get("https://www.instagram.com")
    cookies = pickle.load(open("cookies.pkl","rb"))
    for cookie in cookies:
        if cookie['domain'] == ".instagram.com":
            try:
                driver.add_cookie(cookie)
            except Exception as e:
                print(e)
    driver.get("https://www.instagram.com/p/ClZj_e4vZCF/")
    time.sleep(4)
    wait = WebDriverWait(driver, 10)
    comment_box = wait.until(EC.presence_of_element_located((By.XPATH, '//textarea[@aria-label="Agrega un comentario..."]')))
    comment_box.send_keys(datos['comentario'])

And give me this error:

Traceback (most recent call last):
  File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 42, in <module>
    Comentarios()
  File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 38, in Comentarios
    comment_box.send_keys(datos['comentario'])
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 233, in send_keys
    self._execute(
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 410, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
    (No symbol) [0x00F36643]
    (No symbol) [0x00ECBE21]
    (No symbol) [0x00DCDA9D]
    (No symbol) [0x00DD09E4]
    (No symbol) [0x00DD08AD]
    (No symbol) [0x00DD0B30]
    (No symbol) [0x00DFC474]
    (No symbol) [0x00DFB7AB]
    (No symbol) [0x00E1FD7C]
    (No symbol) [0x00DF641F]
    (No symbol) [0x00E200D4]
    (No symbol) [0x00E36B09]
    (No symbol) [0x00E1FB76]
    (No symbol) [0x00DF49C1]
    (No symbol) [0x00DF5E5D]
    GetHandleVerifier [0x011AA142 2497106]
    GetHandleVerifier [0x011D85D3 2686691]
    GetHandleVerifier [0x011DBB9C 2700460]
    GetHandleVerifier [0x00FE3B10 635936]
    (No symbol) [0x00ED4A1F]
    (No symbol) [0x00EDA418]
    (No symbol) [0x00EDA505]
    (No symbol) [0x00EE508B]
    BaseThreadInitThunk [0x7612FEF9 25]
    RtlGetAppContainerNamedObjectPath [0x770A7BBE 286]
    RtlGetAppContainerNamedObjectPath [0x770A7B8E 238]

CodePudding user response:

if you taking an error you should try this

from selenium import webdriver
from selenium.webdriver.common.by import By

web = webdriver.Chrome()

web.get("https://www.instagram.com/ 'you should write post's url' ")
web.find_element(By.CLASS_NAME,By.CSS_SELECTOR,By.XPATH)
                    #You should choose 
                    #the necessary one and give 
                    #the necessary information with commas. 

CodePudding user response:

As I can see in your code, you have import for WebDriverWait but you are not using it. You can try below solution.

wait = WebDriverWait(driver, 10)
    comment_box = wait.until(EC.presence_of_element_located((By.XPATH, '//textarea[@aria-label="Add a comment..."]')))
    comment_box.send_keys(data['comment'])

OR

comment_box = driver.execute_script("return document.querySelector('textarea[aria-label=\"Add a comment...\"]');")
comment_box.send_keys(data['comment'])
  • Related