【问题标题】:Selenium Discord channel select last messageSelenium Discord 频道选择最后一条消息
【发布时间】:2022-11-13 21:35:38
【问题描述】:

我正在尝试使用 selenium 在不和谐频道上选择最后一条消息 我找不到xpath。 xpath 每次都在更改最后一条消息。我只想复制频道上的最后一条消息。最后一条消息 ID 更改。请帮我。此代码正在记录并轮换到 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)

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    您可以使用此 xpath 通过 xpath 查找您的元素:

    //ol[@data-list-id="chat-messages"]/li[last()]//div[contains(@class,'messageContent')]
    

    解释 xpath

    1. //ol[@data-list-id="chat-messages"] --> 一个 id 为 chat-messagesol 元素(这是消息列表)
    2. /li[last()] --> 我们从上一个列表中得到最后一个 li 元素(这是最后一条消息)
    3. //div[contains(@class,'messageContent')] --> 在前面的 li 中,我们有一个 div 类包含 messageContent,这是包含文本的元素。

      因此,从该元素中获取文本,您应该会收到最新消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 2011-07-03
      • 1970-01-01
      • 2019-05-10
      • 2017-02-20
      • 1970-01-01
      • 2021-03-24
      • 2021-01-19
      相关资源
      最近更新 更多