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?
//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svgyou are trying to locate ansvg nodewhich 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.