Home > OS >  Selenium Discord channel select last message
Selenium Discord channel select last message

Time:11-13

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

  1. //ol[@data-list-id="chat-messages"] --> An ol element with id chat-messages (Which is the list of messages)
  2. /li[last()] --> We get the last li element from the previous list (Which is the last message)
  3. //div[contains(@class,'messageContent')] --> Inside the previous li we have a div which class contains messageContent which is the element that contains the text.

So, take the text from that element and you should get the latest message.

  • Related