0

I'm currently working on an instagram bot, in first, I try to send automatic message using selenium. When the program arrives to the line:

enter_username = WebDriverWait(self.bot, 20).until(expected_conditions.presence_of_element_located((By.NAME, 'username')))

I get this error:

Traceback (most recent call last):
  File "main.py", line 121, in <module>
    init()
  File "main.py", line 115, in init
    bot('reuvenidaniel', 'bbhtk100', user, message)
  File "main.py", line 46, in __init__
    self.login()
  File "main.py", line 52, in login
    enter_username = WebDriverWait(self.bot, 20).until(
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
#0 0x7ff145b83a23 <unknown>
#1 0x7ff14564ee18 <unknown>
#2 0x7ff145685151 <unknown>
#3 0x7ff145685311 <unknown>
#4 0x7ff1456b7ec4 <unknown>
#5 0x7ff1456a29bd <unknown>
#6 0x7ff1456b5c5b <unknown>
#7 0x7ff1456a2883 <unknown>
#8 0x7ff1456783fa <unknown>
#9 0x7ff1456794c5 <unknown>
#10 0x7ff145bb316d <unknown>
#11 0x7ff145bc95bb <unknown>
#12 0x7ff145bb4e75 <unknown>
#13 0x7ff145bc9e85 <unknown>
#14 0x7ff145ba886f <unknown>
#15 0x7ff145be4ae8 <unknown>
#16 0x7ff145be4c68 <unknown>
#17 0x7ff145bffaad <unknown>
#18 0x7ff1450c6609 <unknown>

Here is my code:

from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

ser = Service("/home/danielr/.wdm/drivers/chromedriver/linux64/97.0.4692.71/chromedriver")
op = webdriver.ChromeOptions()
op.add_argument('headless')
op.add_argument('--no-sandbox')
op.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=ser, options=op)

class bot:
  
    def __init__(self, username, password, user, message):
        self.username = username
        self.password = password       
        self.user = user
        self.message = message
        self.base_url = 'https://www.instagram.com/'.
        self.bot = driver
        self.login()

    
        def login(self):
            self.bot.get(self.base_url)
            enter_username = WebDriverWait(self.bot, 20).until(expected_conditions.presence_of_element_located((By.NAME, 'username')))
            enter_username.send_keys(self.username)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • If you disable the headless mode, do you see the instagram home page opened correctly? – Prophet Feb 02 '22 at 09:07
  • its likely that instagram is detecting that this is a bot, and just isn't loading the login component. Try setting a reasonable user-agent or creating a chrome profile with the actual browser, visit instagram, then use that profile with selenium – Dillon Davis Feb 02 '22 at 09:09
  • @Prophet No, it dosen't wotk. I got the same error... – Daniel Reuveni Feb 02 '22 at 09:13
  • @DanielReuveni You are not answering my question – Prophet Feb 02 '22 at 09:16

1 Answers1

0

The username field on Instagram login page is a ReactJS element. So to send a character sequence instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(self.bot, 20).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys(self.username)
    
  • Using XPATH:

    WebDriverWait(self.bot, 20).until(expected_conditions.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys(self.username)
    

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

References

You can find a couple of relevant detailed discussions in:

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