I should start off mentioning I am pretty new to python and selenium, so I don’t really know much about either of them.
what I am trying to do:
- Check every second if a telegram channel I follow has sent out a new message. I have telegram web opened on a browser opened with custom flags so that I don’t need to log in with telegram every single time.
So far what I did is see the class that has all the message’s text inside of it
im_message_text
, and get the last of them in the list.
Source Code
NOTE The "strong" tag is simply where the title is stored in side the text message, which is what i’m using to compare everything:
def getItemsInfo1(self):
try:
iteminfo1 = WebDriverWait(self.driver, 4).until(
EC.presence_of_element_located((By.CLASS_NAME, 'im_message_text')))
list3 = self.driver.find_elements_by_class_name("im_message_text")
list33 = list3[-1]
list4 = list33.find_element_by_tag_name("strong")
list4txt = list4.text
return list4txt
except:
print("Nothing is found")
What I do from here is I make another identical code which has a time.sleep(1)
at the start to give the message time to appear, and i compare the 2 using the following while loop:
while Main.getItemsInfo1() == Main.getItemsInfo2():
print("No new messages. Try number ", count)
print(Main.getItemsInfo1(), Main.getItemsInfo2())
count += 1
So far, whilst as soon as there is a new message the code output highlights that the 2 methods have different text, it still does not end the while loop and it continues to say that there aren’t any new messages.
Does anyone know how to help?
2
Answers
You don’t even need to use telegram web or selenuim either, let’s make it much more easier:
Just remember to change the variables in the
TelegramClient
, You can receive API_ID, API_HASH from Telegram website.They should be like this:
When you call getItemsInfo1() you are calling the method again , so there is no guarantee that the value is get in print statement is same as what is there in while so use something like:
The above code will make sure you are having same value, as you are storing the value to a variable and then validating it .