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
Um in your code you have typed:
and in the error message you it is:
And a quick awsner for your problem is that you try to call a string as a function
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
:or
driver.find_element(By.XPATH, "...")
:Hope that helps!
You are using the Selenium-Python clients, where as this line of code:
is of Selenium-Java clients. Hence the
TypeError
.Solution
Your effective line of code will be:
In a sigle line: