Instagrambot: When trying to like instagram photo, class is not found

Asked

Viewed 109 times

0

I started to develop an API for instagram. Everything is working normally, however the line code that like the publication is not finding the class.

The class: inserir a descrição da imagem aqui

Code to enjoy:

        def curtir_fotos(self, hashtag):
        driver = self.driver
        self.hashtag = hashtag
        driver.get('https://www.instagram.com/explore/tags/' +hashtag+ '/')
        time.sleep(5)
        for i in range(1, 3):
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(3)
        hrefs = driver.find_elements_by_tag_name('a')
        pic_hrefs = [elen.get_attribute('href') for elen in hrefs]
        [href for href in pic_hrefs if hashtag in href]
        print(hashtag + ' fotos: ' +str(len(pic_hrefs)))

        for pic_hrefs in pic_hrefs:
            driver.get(pic_hrefs)
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                driver.find_element_by_class_name('//button[@class="_8-yf5 "]').click()
                time.sleep(10)
            except Exception:
                time.sleep(5)

He always falls in the except and I don’t know why. So how do I solve??

2 answers

1

Hello, try to change: driver.find_element_by_class_name('//button[@class="_8-yf5 "]').click()

for: driver.find_element_by_class_name('_8-yf5').click()

For I believe this method you are using '//button[@class="_8-yf5"]' is only for xpath

And if you’re going to try with xpath, try something like '//svg[@aria-label="Curtir"]'

  • Thank you very much for the answer, but unfortunately it didn’t work... I’ve tried it in many ways and it doesn’t work. I’m really ready to give up kkk just click the button to complete and so far nothing.But I’ll keep trying.

  • but what kind of error does it present? It can make us available?

  • No explicit error appears, but when you try to click it simply goes to next post without liking it... it’s as if it doesn’t find the element or can’t run the Butão

  • is that actually he has to click the button, the instruction I gave you click the Like icon. Maybe this is the current problem

  • So to resolve should I click on the information where is written Button?? Sorry is that we say html is not my beach kk but I joined the area invader to do this project.

  • I believe that this case of yours is this situation. We would have to test to be sure.

  • Would you like me to send the full code?

Show 2 more comments

0


Hello, could you try with the driver.find_element_by_css_selector

Would look like this: driver.find_element_by_css_selector("#react-root > section > main > div > div.ltEKP > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button > div > span > svg").click()

I tested and it worked.

To capture the css Right-click the element there in Chrome then Inspect Usa Ctrl+Shirt+C and selects the element then right-click on the code that was marked next to the next step element Copy and finally Copy Selector, now and just play there in the code.

  • Thanks friend! To tell you the truth I tested this one and it didn’t work at first, but discover the problem and I feel like a real clown. The truth is, when I run the code driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") for the second time inside the for with the pic_hrefs, it descends and with this it ends up not finding the class or even element requested... I am truly happy and angry with this, but I really appreciate you helping me, after all it was from this that I discover the mistake. Thank you again and good night!

  • The good thing about programming is that the mistakes without them do not discover the solution within the lines.

  • I’m still very new to this, but very new. and a good thing is the community that helps each other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.