skip to Main Content

I’m trying to scrape a telegram channel by going to the last message and print the text inside it / store it in a variable to use it later.

Code trials:

from cgitb import text
from http import server
from re import search
import selenium
import time
import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)

driver.get("https://t.me/s/klfjezlkjfzlek")
test = driver.find_element(By.XPATH("//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]"))
source_code = test.text
print(source_code)

I get the following error :

Traceback (most recent call last): 
File "/Users/usr/Desktop/tg.py", line 16, in <module>
text=driver.find_element(By.XPATH("//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]"))     
TypeError: 'str' object is not callable

3

Answers


  1. Um in your code you have typed:

    test = driver.find_element(By.XPATH("//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]"))
    

    and in the error message you it is:

    text=driver.find_element(By.XPATH("//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]"))
    

    And a quick awsner for your problem is that you try to call a string as a function

    Login or Signup to reply.
  2. That would be because By.XPATH is actually a string. Here’s some further reading from Selenium’s docs.

    You may use find_element_by_xpath:

    test = driver.find_element_by_xpath("//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]")
    

    or driver.find_element(By.XPATH, "..."):

    test = driver.find_element(By.XPATH, "//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]")
    

    Hope that helps!

    Login or Signup to reply.
  3. You are using the Selenium-Python clients, where as this line of code:

      driver.find_element(By.XPATH("//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]"))
      
    

    is of Selenium-Java clients. Hence the TypeError.


    Solution

    Your effective line of code will be:

    test = driver.find_element(By.XPATH, "//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]")
    source_code = test.text
    print(source_code)
    

    In a sigle line:

    print(driver.find_element(By.XPATH, "//html//body//main//div//section//div//div//div//div[@class='tgme_widget_message_text js-message_text before_footer'][last()]").text)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search