Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to locate element xpath #13

Closed
arda1984 opened this issue Oct 21, 2021 · 16 comments
Closed

unable to locate element xpath #13

arda1984 opened this issue Oct 21, 2021 · 16 comments

Comments

@arda1984
Copy link

I get this error when I try to run Instagrambot:

"D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\Scripts\python.exe" C:/Users/ebrul/Desktop/InstagramBot/instagramBot.py
Traceback (most recent call last):
  File "C:/Users/ebrul/Desktop/InstagramBot/instagramBot.py", line 89, in <module>
    start()
  File "C:/Users/ebrul/Desktop/InstagramBot/instagramBot.py", line 66, in start
    acceptCookies = browser.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/button[1]');
  File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\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 "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\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":"/html/body/div[2]/div/div/div/div[2]/button[1]"}
  (Session info: chrome=95.0.4638.54)


Process finished with exit code 1

It just opens https://www.instagram.com/accounts/login/?source=auth_switcher with chrome and that's it.
the only thing I've changed is the comments, webdriver dir, and my account info. How can I fix this?

@Radulepy
Copy link
Owner

Radulepy commented Oct 21, 2021

Hi,

So the problem with this bot is that it has to be changed with every Instagram update.
Your error (line 66) is searching for the username field. You can find the X path of the field by going to instagram site, choosing the wanted element (our case: username field), right click on the field and click inspect. Next, on the outlined element (input field, in the inspect elementor) just right click, and copy Full Xpath.

You will have this:
/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input

Next, replace line 66:
username = browser.find_element_by_name('username')
(which finds the field with the username in it)
with
username = browser.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input');
(which finds the field with the Xpath)

you will probably need to replace all the lines that were changed by instagram (password).

Remember, you could also find the wanted element by name but it's easier to right-click copy Xpath and paste it there.

Please come back with a feedback for this. Or write further any errors and I'll try to help you.

@arda1984
Copy link
Author

Thank you, I will try it later

@arda1984
Copy link
Author

arda1984 commented Oct 22, 2021

Thank you now I can log in, and I also figured out how I can let it close the notification and "save account information" popups. But I still don't know how this comment function works, I changed the explore link to my feed and also to the xpath of the comment line but it says that its unable to locate element

@arda1984
Copy link
Author

code.txt
So this is the whole code.
I can log in, close the popups, and like the first post that comes to my feed, but I can't comment. In python, it says that the element is not interactable, it has to be something with the comment line, could you help me?

@Radulepy
Copy link
Owner

@arda1984
Sorry, I did not get all the notification.

I'm glad you figured it out.

For the comment section try taking the underlying elements from the comment section. Take the textarea element and not the div (since you should write text to it)

For example now, the comment textarea element has this xPath:
/html/body/div[6]/div[2]/div/article/div/div[2]/div/div/div[2]/section[3]/div/form/textarea

As you can see there are some arrays like this section[3]. These arrays can change with the post but I would suggest starting from that and going upwards. If the above xPath for comment element does not work try writing directly on the form by removing the textarea element from the xPath. Thus your element xPath will be this:

/html/body/div[6]/div[2]/div/article/div/div[2]/div/div/div[2]/section[3]/div/form

Waiting for your feedback!

@Radulepy Radulepy pinned this issue Oct 26, 2021
@arda1984
Copy link
Author

Hey Radule, so yesterday I fixed the comment line, so now I can log in, close the popups, and like & comment (random comments is also working) on the first post that comes to my feed but how is it going to do the same thing on the next 8 posts and reload the feed page so there will be new posts shown?
codeupdated.txt
:)

@arda1984
Copy link
Author

@Radulepy

@Radulepy
Copy link
Owner

Radulepy commented Oct 28, 2021

@arda1984
Hi, I'm glad you did it!

To refresh the page you just have to reload the feed page:
browser.get('https://www.instagram.com/explore/')
It was already doing it after iterating trough posts.

To comment and like the other post you have to iterate trough the elements with a for
In my code you can see starting from line 28 the 2 for's.

The most important line is 30:
post = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div/div['+str(y)+']/div['+str(x)+']')

This line iterates the posts from 1 to 4 like this:
post = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div/div[1]/div[1])
post = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div/div[1]/div[2])
post = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div/div[1]/div[3])
post = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div/div[1]/div[4])
post = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/div/div[2]/div[1])
...

As you can see, the last div is increasing with x and the previous with y. These are the post xPath. You have to find the differences between the xPath of the IG posts and change accordingly.

Good luck!

@arda1984
Copy link
Author

@Radulepy
It almost works, it likes and comments on the first 2 posts on the feed page, but on the third one it just likes it and stops with the error;

File "C:/Users/ebrul/Desktop/InstagramBot-xy/instagramBotxy.py", line 87, in <module> start() File "C:/Users/ebrul/Desktop/InstagramBot-xy/instagramBotxy.py", line 80, in start likeAndComm() # likeAndComm function ---------------------------------------------------------------------------------------------------------------------------------------------------------- File "C:/Users/ebrul/Desktop/InstagramBot-xy/instagramBotxy.py", line 35, in likeAndComm postLike = browser.find_element_by_xpath('/html/body/div[1]/section/main/section/div/div[2]/div/article['+str(x)+']/div/div[3]/div/div/section[1]/span[1]/button').click() File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click self._execute(Command.CLICK_ELEMENT) File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute return self._parent.execute(command, params) File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "D:\Programme\JetBrains\PyCharm Community Edition 2021.2.1\env\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="wpO6b " type="button">...</button> is not clickable at point (69, 20). Other element would receive the click: <img alt="Instagram" class="s4Iyt" src="/static/images/web/mobile_nav_type_logo.png/735145cfe0a4.png" srcset="/static/images/web/mobile_nav_type_logo-2x.png/1b47f9d0e595.png 2x">

I did some research and tried to change some lines too but it didn't work

@arda1984
Copy link
Author

@Radulepy did you got the notification? :)

@arda1984
Copy link
Author

arda1984 commented Nov 3, 2021

💀

@Radulepy
Copy link
Owner

Radulepy commented Nov 6, 2021

@arda1984, so it can't find the element to click (input). Be sure to only add the input (textarea) element to the xPath.
Perhaps the 3rd element has a different xPath than the previous ones.

@arda1984
Copy link
Author

arda1984 commented Nov 6, 2021

@Radulepy

/html/body/div[1]/section/main/section/div/div[2]/div/article[2]/div/div[3]/div/div/section[3]/div/form/textarea
/html/body/div[1]/section/main/section/div/div[2]/div/article[3]/div/div[3]/div/div/section[3]/div/form/textarea

The only difference between those is the article number where I put the x variable and it still doesn't work

@arda1984
Copy link
Author

@Radulepy Sorry but its been a week

@Radulepy
Copy link
Owner

@Radulepy

/html/body/div[1]/section/main/section/div/div[2]/div/article[2]/div/div[3]/div/div/section[3]/div/form/textarea
/html/body/div[1]/section/main/section/div/div[2]/div/article[3]/div/div[3]/div/div/section[3]/div/form/textarea

The only difference between those is the article number where I put the x variable and it still doesn't work

You should have something different depending on the post. Watch out you are pointing to the textarea not the post.

html/body/div[1]/section/main/section/div/div[2]/div/article[2]/div/div[3]/div/div/section[3]/div/form/textarea
html/body/div[1]/section/main/section/div/div[2]/div/article[2]/div/div[3]/div/div/section[2]/div/form/textarea
html/body/div[1]/section/main/section/div/div[2]/div/article[2]/div/div[3]/div/div/section[1]/div/form/textarea

something incrementing will point out that the post is changing.

@Radulepy
Copy link
Owner

@arda1984 Check the latest update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants