skip to Main Content

How to send a message when a webpage text with Selenium in Python?

Hello, I am a python beginner.
I want to send a message with telegram in Python.
I got a text to send message from webpage.
How can I send a telegram message?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import telegram

driver = webdriver.Chrome()
url = "http://webpage/board/1234"
driver.get(url)

notice=driver.find_element_by_class_name("viewBox") 
bot=telegram.Bot('My token')
bot.send_message('My channel', 'notice')

I want to send the text in the viewbox as a message.
I got the text in the viewbox with selenium.
I made a telegram bot and also confirmed that it works.
How can I send the text as a telegram message?

2

Answers


  1. Chosen as BEST ANSWER
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    import telegram
    
    driver = webdriver.Chrome()
    url = "http://webpage/board/1234"
    driver.get(url)
    
    notice=driver.find_element_by_class_name("viewBox")
    
    bot=telegram.Bot('My token')
    bot.send_message(chat_id="My chat id", text=notice.text)
    

  2. The short answer is

    bot.send_message('My channel', notice.text)
    

    To find it you could type dir(notice) to see the element’s attributes. And of course you can check the Selenium API documentation for the Element object.

    Also, given that Element represents a web page object and it probably uses the same naming of javascript, you could open the dev console on Chrome (ctrl-shift-J) and type this:

    // load Google Home first
    a = document.getElementsByClassName('gb_g')[0]
    a.text
    > Gmail
    

    As you probably know, the console is useful to be sure that the text you’re looking for is actually in that viewBox

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search