1

I want to upload a photo on Instagram using selenium. I write this code but gives me an error .

Code:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("username")

my_password=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("*******")
time.sleep(10)

login=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
login.click()

time.sleep(5)
upload=driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svg')
upload.click()
img='"C:/Users/lucky/Downloads/old-black-background-grunge-texture-dark-wallpaper-blackboard-chalkboard-room-wall.jpg"'
time.sleep(2)
img_upload=driver.find_element_by_xpath('/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/div/div/div[2]/div/button')

img_upload.send_keys(img)

time.sleep(30)

Output:

====== WebDriver manager ======
Current google-chrome version is 97.0.4692
Get LATEST chromedriver version for 97.0.4692 google-chrome
Driver [C:\Users\lucky\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe] found in cache
d:\Selenium\main.py:10: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

DevTools listening on ws://127.0.0.1:51042/devtools/browser/31fa564f-7dda-4d59-beaa-8e7cb8430135
d:\Selenium\main.py:13: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  my_email=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
d:\Selenium\main.py:16: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  my_password=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
d:\Selenium\main.py:20: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  login=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
d:\Selenium\main.py:24: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  upload=driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svg')
Traceback (most recent call last):
  File "d:\Selenium\main.py", line 24, in <module>
    upload=driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svg')
  File "C:\Users\lucky\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 520, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\lucky\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\lucky\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\lucky\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svg"}  
  (Session info: chrome=97.0.4692.99)
Stacktrace:
Backtrace:
        Ordinal0 [0x0079FDC3+2555331]
        Ordinal0 [0x007377F1+2127857]
        Ordinal0 [0x00632E08+1060360]
        Ordinal0 [0x0065E49E+1238174]
        Ordinal0 [0x0065E69B+1238683]
        Ordinal0 [0x00689252+1413714]
        Ordinal0 [0x00677B54+1342292]
        Ordinal0 [0x006875FA+1406458]
        Ordinal0 [0x00677976+1341814]
        Ordinal0 [0x006536B6+1193654]
        Ordinal0 [0x00654546+1197382]
        GetHandleVerifier [0x00939622+1619522]
        GetHandleVerifier [0x009E882C+2336844]
        GetHandleVerifier [0x008323E1+541697]
        GetHandleVerifier [0x00831443+537699]
        Ordinal0 [0x0073D18E+2150798]
        Ordinal0 [0x00741518+2168088]
        Ordinal0 [0x00741660+2168416]
        Ordinal0 [0x0074B330+2208560]
        BaseThreadInitThunk [0x76FC6739+25]
        RtlGetFullPathName_UEx [0x77CF8AFF+1215]
        RtlGetFullPathName_UEx [0x77CF8ACD+1165]

At first, I think I give the wrong XPath and then I try again with a right and it gives me this error. Can you guys tell me what's wrong with the code? And my chrome also get closed as soon as the code end so I need to put time sleep over there . Is there any way to minimize that things also?

2
  • here //*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svg you are trying to locate an svg node which does not work as standard xpath expression. You will have to write //*[name()='svg'], Update the question with relevant HTML for better understanding or provide the steps that you are automating once you are logged in to the instagram. Commented Feb 6, 2022 at 13:02
  • //*[name()='svg'] can you explain this more to me .I only get it half . Commented Feb 6, 2022 at 16:59

1 Answer 1

1

try this

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import pyautogui

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("------")

my_password=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("-------")
time.sleep(10)

login=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
login.click()

time.sleep(5)
upload=driver.find_element_by_xpath('//div[@class="QBdPU "]')
upload.click()
time.sleep(2)
img_upload=driver.find_element_by_xpath('/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/div/div/div[2]').click()
time.sleep(2)
path = "D:\Pictures\ph.png" # your imagepath
pyautogui.write(path) 
time.sleep(2)
pyautogui.press('enter')


more steps need to be done for the full upload process.

suggestion instead of time.sleep use Waits

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot .Your code fixed my bug .But can you explain this lineupload=driver.find_element_by_xpath('//div[@class="QBdPU "]') why you choose this xpath .
I just didn't want to click the SVG, rather I interacted with the div it was in, it's much easier I guess. And Wrote XP manually rather than copping from the chrome dev tools. Because dynamic websites are easier to handle this way. I hope you understand
I want to use this code on the web app, do you any way I can implement this code to my website, and when I give input in my browser I can directly upload that to an Instagram.
Yes you can do that I guess but I don't have a clear picture how you can achieve this goal. But there are many websites that automate your posts both on website's like WordPress and Instagram. It's better you do your own research .

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.