O
O
OgnevEgor2021-09-27 22:13:57
Python
OgnevEgor, 2021-09-27 22:13:57

Why is Selenium not finding the element?

I'm trying to automate hashtag likes on insta, but when it comes to liking I get the following error (selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:{"method":"xpath", "selector":"/html/body/div[1]/section/main/div/div[1]/article/div/div[3]/div/div/section[1]/span[1]/button "}
(Session info: chrome=94.0.4606.61))


Python code:

import random
import time
from selenium.webdriver.common.keys import Keys

from auth import login, password
from selenium import webdriver
from selenium.webdriver.common.by import By

def login_chrome (login, passwrod):
    global browser
    browser = webdriver.Chrome()
    browser.get("https://www.instagram.com/?hl=ru")
    time.sleep(random.randrange(2,4))
    _login = browser.find_element(By.NAME, "username")
    _login.clear()
    _login.send_keys(login)
    time.sleep(random.randrange(2,4))
    _password = browser.find_element(By.NAME, "password")
    _password.clear()
    _password.send_keys(password)
    time.sleep(random.randrange(2,4))
    _password.send_keys (Keys.ENTER)


def hashtag (hashtag):
    global browser
    browser.get(f"https://www.instagram.com/explore/tags/{hashtag}/")
    time.sleep(2)

    for i in range (1,5):
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(2)

    hrefs = browser.find_elements_by_tag_name("a")
    urls = [item.get_attribute('href') for item in hrefs if "/p/" in item.get_attribute('href')]
    print(urls)
    for url in urls:
        browser.get(url)
        time.sleep(random.randrange(2, 5))
        like = browser.find_element(By.XPATH,"/html/body/div[1]/section/main/div/div[1]/article/div/div[3]/div/div/section[1]/span[1]/button")
        like.click()
        time.sleep(random.randrange(85, 105))








login_chrome(login, password)
time.sleep(5)
hashtag ("beautiful")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-09-27
@SoreMix

Because there <main>are no tags <div>. Click on the path in the browser, it takes 15 seconds

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question