'Message: no such element: Unable to locate element: {"method":"xpath"
I write this script for autoliking instagram posts by fake users.In loop for first time script works and can find button element for click.but from second time it doesnt work and get an error.no such element: Unable to locate element.how can i solve this problem?please help.I`m new in python and selenium.
import random
from selenium import webdriver
from fake_useragent import UserAgent
from selenium.webdriver.common.by import By
from selenium. webdriver. common. keys import Keys
import time as t
import pickle
from readybots import bot
from proxylist import proxy
from InstaAuth import InstaAuthorization
useragent=UserAgent()
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument(f"user-agent={useragent.random}")
options.add_argument(f"--proxy-server={random.choice(proxy)}")
driver=webdriver.Chrome(executable_path="C:\\Users\\USER\\PycharmProjects\\Seleniumtest\\chromedriver\\chromedriver.exe",options=options)
q=InstaAuthorization()
i=1
a=0
for log,pwd in bot.items():
try:
options.add_argument(f"user-agent={useragent.random}")
options.add_argument(f"--proxy-server={random.choice(proxy)}")
driver = webdriver.Chrome(options=options)
driver.get(url="https://instagram.com/")
driver.implicitly_wait(10)
driver.maximize_window()
print("instagram opened")
t.sleep(random.randrange(1, 2))
q.sign_in_cook(log,driver) #add cookie
print("cookie")
t.sleep(random.randrange(1,4))
driver.get(url="https://www.instagram.com/p/CcNgsJkKm0Y/")
driver.implicitly_wait(15)
t.sleep(random.randrange(8,12))
like=driver.find_element(By.XPATH,'//*[@id="react-root"]/div/div/section/main/div/div[1]/article/div/div[2]/div/div[2]/section[1]/span[1]/button')
t.sleep(random.randrange(5, 8))
like.click()
print(driver.session_id)
print(f"{i}liked")
driver.delete_all_cookies()
i = i + 1
driver.close()
except Exception as ex:
print(driver.session_id)
driver.delete_all_cookies()
print(ex)
print(log)
i-=1
print(i)
print(a)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
