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

self.driver.find_elements returns with [] can't find the text #3

Closed
ghost opened this issue Feb 21, 2023 · 17 comments
Closed

self.driver.find_elements returns with [] can't find the text #3

ghost opened this issue Feb 21, 2023 · 17 comments

Comments

@ghost
Copy link

ghost commented Feb 21, 2023

instagrambot.py messages_element = self.driver.find_elements(By.CLASS_NAME, username) does not return as expected. Therefore, the codes are stuck in while loop as there is no message

@JoseHenriqueSiqueira
Copy link
Owner

Are you using GetMessager() or BotStart()? And it doesn't return any kind of error?

@ghost
Copy link
Author

ghost commented Feb 21, 2023

I use it without changing. Botstart(). It doesn't return anything as text is empty that cause infinite loop. I also used different username, and got another error that closes the pages when message section is clicked.

GetMessager() for my understanding is not applied anywhere

@JoseHenriqueSiqueira
Copy link
Owner

Are you passing the parameters in the correct order? First comes the username and then the APIKEY.
Getting like this:

python InstagramBot.py <user> <apikey>

I've just tested the bot here, and it's still working normally.
Try putting some prints() to get the value of USER and API_KEY variables and then check if the user is passed correctly, as well as the APIKEY.

if __name__ == "__main__":
    if len(sys.argv) < 3:
        raise Exception("USER and API_KEY is mandatory and must be passed as an argument.")
    USER = sys.argv[1]
    API_KEY = sys.argv[2]
    print("Instagram User: ", USER)
    print("API_KEY: ", API_KEY)
    BotGPT = ChatBot(API_KEY)
    InstBOT = InstagramBotGPT(user=USER, chatbot=BotGPT)
    InstBOT.BotStart()

@ghost
Copy link
Author

ghost commented Feb 22, 2023

No, unfortunately, I already used it in this way. I have used it on three PCs (mac and windows), it didn't work out. Which google chrome version you used?

@JoseHenriqueSiqueira
Copy link
Owner

I use version 106.0.5249.103. Does your browser simply close or is the bot unable to respond? Maybe instagram is different in your region. Can you send a screenshot of the steps or where the script break?

@ghost
Copy link
Author

ghost commented Feb 22, 2023 via email

@JoseHenriqueSiqueira
Copy link
Owner

can you open the script and try to use the getmessanger method? it is exactly for debug. It will check if it is capturing user or author messages.

if __name__ == "__main__":
    InstBOT = InstagramBotGPT(user='type_any_user_here', chatbot=None)
    author_msg = InstBOT.GetMessager('author')
    user_msg = InstBOT.GetMessager('user')
    print("\nAUTHOR_MESSAGES:", author_msg)
    print("\nUSER_MESSAGES:", user_msg)

That way you don't need to pass the parameters. Try using it like this and check if any of the variable values ​​are printed:

python InstagramBot.py

By the way, check that the user is correct with the Instagram one. User example:
https://www.instagram.com/user123/.
so, copy 'user123'.

About your second question:
Yes, that's the purpose of the bot itself. You enter a target user as a parameter and then the conversation is captured in a loop. After the target user sends a message, the script captures this message and stores it in a variable and then sends it to the Bot. When the bot returns the answer, then it is passed to the element responsible for sending messages on instagram and finally it is sent to the target user through the simulation of the Enter key.

@ghost
Copy link
Author

ghost commented Feb 22, 2023

Yes, I have tried it and it gives empty text and It did not open messages at all.

And this is the output after the page is closed automatically.

AUTHOR_MESSAGES: []

USER_MESSAGES: []

I guess the problem is related to BY.CLASS_NAME. I will also search this out to capture the texts. Thanks for the debugging clue.

For the second question, I was meaning that a more autonomous way to do that without entering the target name. E.g. Just the author username is entered and the bot is alive. So it can answer whoever asks a question.

@JoseHenriqueSiqueira
Copy link
Owner

JoseHenriqueSiqueira commented Feb 22, 2023

Can you provide me with the full version of chrome you are trying? So I add it to the repository that is malfunctioning with that specific version.

About your question about the bot, yes it is possible. With a little web scraping you can do it. I would try as follows.
1 - Ask some users to send a message to your account.
2 - Then you will inspect the elements and look for what differs an element that contains unread messages, to an element that contains a read message. Possibly a different specific class.
2 - look for the class responsible for the users' messages.
3 - and the rest is basically already in the InstagramBot. Hope this helps you.

@ghost
Copy link
Author

ghost commented Feb 22, 2023

Version 110.0.5481.100, I am not sure if it is related to a google chrome version though. Thanks anyway for your comments.

@JoseHenriqueSiqueira
Copy link
Owner

I think it's some version of Instagram. I could fix it for you, I'll try to find an alternative class to get the text.

@JoseHenriqueSiqueira
Copy link
Owner

Version 110.0.5481.100, I am not sure if it is related to a google chrome version though. Thanks anyway for your comments.

I've updated the repository and added selinium functionality that waits for the element to be loaded before proceeding. Perhaps the problem was that your script ran before the class itself loaded. Try using prints() again in GetMessager()

@ghost
Copy link
Author

ghost commented Feb 24, 2023

File "/Users/erdogan/PycharmProjects/Whatsapp_Instagram_Messanger_ChatGPT_OpenAI/InstagramBot.py", line 66, in <module>
    author_msg = InstBOT.GetMessager('author')
  File "/Users/erdogan/PycharmProjects/Whatsapp_Instagram_Messanger_ChatGPT_OpenAI/InstagramBot.py", line 50, in GetMessager
    self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, UserType)))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
0   chromedriver                        0x000000010ad98138 chromedriver + 4923704
1   chromedriver                        0x000000010ad109d3 chromedriver + 4368851
2   chromedriver                        0x000000010a959787 chromedriver + 472967
3   chromedriver                        0x000000010a99dba3 chromedriver + 752547
4   chromedriver                        0x000000010a99ddf1 chromedriver + 753137
5   chromedriver                        0x000000010a9e4734 chromedriver + 1042228
6   chromedriver                        0x000000010a9c6fdd chromedriver + 921565
7   chromedriver                        0x000000010a9e1c0f chromedriver + 1031183
8   chromedriver                        0x000000010a9c6d23 chromedriver + 920867
9   chromedriver                        0x000000010a98da4b chromedriver + 686667
10  chromedriver                        0x000000010a98f044 chromedriver + 692292
11  chromedriver                        0x000000010ad638fe chromedriver + 4708606
12  chromedriver                        0x000000010ad68e22 chromedriver + 4730402
13  chromedriver                        0x000000010ad716bf chromedriver + 4765375
14  chromedriver                        0x000000010ad69e80 chromedriver + 4734592
15  chromedriver                        0x000000010ad3a975 chromedriver + 4540789
16  chromedriver                        0x000000010ad8be78 chromedriver + 4873848
17  chromedriver                        0x000000010ad8bff5 chromedriver + 4874229
18  chromedriver                        0x000000010ada06de chromedriver + 4957918
19  libsystem_pthread.dylib             0x00007fff6bc3e109 _pthread_start + 148
20  libsystem_pthread.dylib             0x00007fff6bc39b8b thread_start + 15

I am receiving this now. It exceeds the wait time. I think the problem is the same. For some reason, BY.class_name does not get the text from the inspected class.

@ghost
Copy link
Author

ghost commented Feb 24, 2023

Problem solved. I accidentally changed the user by refoctoring Types = {"author":"_acqv", 'user':'_acqu'} with an instagram user name

@ghost ghost closed this as completed Feb 24, 2023
@JoseHenriqueSiqueira
Copy link
Owner

Now both methods are working and returning messages?

@ghost
Copy link
Author

ghost commented Feb 24, 2023 via email

@JoseHenriqueSiqueira
Copy link
Owner

Right. This issue has been resolved. The other problem I will study a little what could be the cause and simulate some scenarios

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

1 participant