Instagram BOT using InstaPy and Python

160 Views Asked by At

I am trying to make a Python and InstaPy based BOT that logs into Instagram, likes posts and makes comments. Please help in debugging the following code.

The Code is:

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

browser = webdriver.Firefox() 
browser.implicitly_wait(5)

browser.get('https://www.instagram.com/')

login_link = browser.find_element(By.XPATH, "//a[text()='Log in']") 
login_link.click()

sleep(5)

browser.close()

Error Message is:

File "C:\0. D drive\BOT\step2.py", line 10, in login_link = browser.find_element(By.XPATH, "//a[text()='Log in']") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\nadel\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\nadel\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute self.error_handler.check_response(response) File "C:\Users\nadel\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //a[text()='Log in'] Stacktrace: RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8 WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:191:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:509:5 dom.find/</<@chrome://remote/content/shared/DOM.sys.mjs:136:16
1

There are 1 best solutions below

0
Harsh On

This code will do the trick:

from time import sleep 
from selenium import webdriver 
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

browser = webdriver.Firefox() 
browser.implicitly_wait(5)

browser.get('https://www.instagram.com/')

login_link = browser.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button') 
action = ActionChains(browser)
action.click(login_link).perform()

sleep(5)

browser.close()