im 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 .
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
CodePudding user response:
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!
CodePudding user response:
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