【问题标题】:Discord chatting Selenium inconsistency不和谐聊天 Selenium 不一致
【发布时间】:2020-12-05 17:04:51
【问题描述】:

我正在使用 selenium 自动将消息发送到预定服务器中的预定不和谐频道。

我成功导航到服务器,但在定位文本通道时出现不一致。 当我尝试在我的服务器上查找特定频道时,它可以毫无问题地找到它,但是当我在其他服务器上查找它时却无法找到它。

我使用以下命令来定位频道:

channel_element = driver.find_element_by_xpath("//div[@id='channels-5']")

在 Chrome 中使用 Inspect 时,我从 discord 网站获得了特定的 id

setup=[{"server":"RL Insider","channel":"channels-23"}, {"server": "Carrot automation server", "channel": "channels-5"}]

import selenium
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://discord.com/channels/@me")

# login

print(driver.title)
input("input anything when logged in")

def go_to_channel(server, channel_id,message):
    server_element = driver.find_element_by_xpath("//a[@aria-label='"+server+"']")
    driver.execute_script("arguments[0].click();", server_element)
    input("prompt")
    print(driver.current_url)
    try:
        channel_element = driver.find_element_by_xpath("//div[@id='"+channel_id+"']") #this does not work when used on the RL Insider server
        driver.execute_script("arguments[0].click();", channel_element)
        input("prompt")
        text_element = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/div/div/div[2]/div/main/form/div/div/div/div/div[3]/div[2]')
        text_element.click()
        text_element.clear()
        for c in message[::-1]:
            text_element.send_keys(c)
        text_element.send_keys(Keys.RETURN)
        input("prompt")
    except selenium.common.exceptions.NoSuchElementException:
        print("not found by id xpath")

for i in setup:
    go_to_channel(i["server"],i["channel"],string)

driver.close()

所以我希望两台服务器的行为相同,因为我无法找到有问题的服务器。

【问题讨论】:

  • 您为什么要导航?您可以只使用频道的网址
  • @KhanSaad 这就是我最终做的事情

标签: python selenium selenium-webdriver discord


【解决方案1】:

处理代码的速度可能不一致。服务器可能有更多的计算能力并更快地运行代码。这样,您尝试查找的元素可能尚未加载。我建议在找到它之前使用等待元素。

这里有一些例子:

https://seleniumbyexamples.github.io/wait

【讨论】:

  • 请将链接中的详细信息添加到答案中。将来可能会破坏链接,这将使答案无效。
  • 添加了链接。谢谢
猜你喜欢
  • 2020-08-20
  • 2021-06-30
  • 2021-12-12
  • 1970-01-01
  • 2021-03-10
  • 2017-12-31
  • 2022-07-12
  • 2021-08-26
  • 2021-09-11
相关资源
最近更新 更多