I am trying select last message on discord channel with selenium I can't find xpath. xpath is changing every time for last message. I just want copy last message on the channel. Last message id changing. Please help me. This code is logging and goes rotate to chanel.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import os, shutil
driver = webdriver.Chrome()
driver.get("https://discord.com/login")
time.sleep(6)
#--------------- Edit Here -------------------------------------------------------------
# Enter your account details here
username = ''
password = ''
# Copy the URL of channel where you wanna send messages and paste below
channelURL = "https://discord.com/channels/775349757060186142/77535656021144289331"
#-------------- Edit End ----------------------------------------------------------------
# Initialize and input email
username_input = driver.find_element_by_name('email')
username_input.send_keys(username)
# Initialize and input password
password_input = driver.find_element_by_name('password')
password_input.send_keys(password)
# Initialize and login
login_button =
login_button.click()
print(">>Login Complete!")
time.sleep(10)
driver.get(channelURL)
print(">Opening The Server Link...")
time.sleep(5)
# Msg Sending
msgoutput = # I can't find last message's xpath
print("last message is")
print(msgoutput)
CodePudding user response:
You can use this xpath to find your element by xpath:
//ol[@data-list-id="chat-messages"]/li[last()]//div[contains(@class,'messageContent')]
Explaining the xpath
//ol[@data-list-id="chat-messages"]
--> Anol
element with idchat-messages
(Which is the list of messages)/li[last()]
--> We get the lastli
element from the previous list (Which is the last message)//div[contains(@class,'messageContent')]
--> Inside the previousli
we have a div which class containsmessageContent
which is the element that contains the text.
So, take the text from that element and you should get the latest message.