0
def getFollowers(self):
    self.browser.get(f"https://www.instagram.com/{self.username}")
    time.sleep(2)
    // I have a problem with the XPath in this line
    self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/ul/li[2]/a").click()
    time.sleep(2)

I'm pretty new to Python. I wrote the exact code described in the lesson above and shared a part of it here. There is a problem with the line after the comment.

What alternative can I use to find_element_by_xpath code? Also, how should it be used?

Although I copied the code from the tutorial, I get errors and I don't understand why. The error is below.

    DevTools listening on ws://127.0.0.1:65233/devtools/browser/f0005e34-6939-40d0-8480-d4ff3ad06845
Traceback (most recent call last):
  File "_instagram.py", line 59, in <module>
    instagrm.getFollowers()
  File "_instagram.py", line 27, in getFollowers
    self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/ul/li[2]/a").click()
  File "C:\Users\sunaercan\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\sunaercan\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\sunaercan\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\sunaercan\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, 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/main/div/ul/li[2]/a"}
  (Session info: chrome=84.0.4147.125)
1
  • Please edit your question to show the HTML you are working with. Also please add a link to the lesson you are using if it does not require a login. It's possible the page layout has changed since the lesson was written so the XPath isn't valid any more.
    – Kate Paulk
    Commented Aug 17, 2020 at 11:51

1 Answer 1

1

If you want to get the number of followers from Instagram page, that xPath seems to be incorrect. It could perhaps change for different user accounts or over time. I know Instagram very little, and definitely not its DOM.

Anyway, I'd suggest avoiding this kind of xPath that is this long. It could break often, which is likely this case. I'd try to find something that seems more stable in the DOM, in this case, it could perhaps be the URL. Taking that into account, the DOM looks like:

<a class="-nal3 " href="/accounts/login/?next=%2abc%2Ffollowers%2F&amp;source=followed_by_list" tabindex="0">
<span class="g47SY " title="4,828" data-tryxpath-element="0">4,828</span> followers</a>

and xPath to get the number of followers could be: //a[contains(@href, 'followers')]/span

3
  • Thank you for your answer. So what code should I use before the usage you mentioned? find_element_by_xpath, find_element_by_n Game or other code? Commented Aug 18, 2020 at 12:42
  • Try to use ids first, css then, xpath is perhaps the last option if nothing else proves rebust and maintainable.
    – pavelsaman
    Commented Aug 18, 2020 at 13:23
  • Ok. I will try them, thank you, very much Commented Aug 18, 2020 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.