【问题标题】:How to use selenium to click on a discord servers channel?如何使用 selenium 点击不和谐服务器频道?
【发布时间】:2020-04-04 18:03:46
【问题描述】:

我正在尝试使用 selenium 和 python 来自动化一些不和谐的任务。我打开浏览器到 discordapp.com,登录,转到有问题的服务器,但我找不到任何 css 选择器、xpath、类名等能够点击名为“commands”的频道。任何帮助,将不胜感激。

from selenium import webdriver
import time
browser = webdriver.Firefox(executable_path=r'C:\Users\levir\OneDrive\Desktop\geckodriver.exe')
browser.get('https://discordapp.com')
linkElem = browser.find_element_by_class_name('appButton-3GZ9-9') #login button
linkElem.click()
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/form/div/div/div/div[3]/div[1]/div/input')
linkElem.send_keys('EMAIL') #email
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/form/div/div/div/div[3]/div[2]/div/input')
linkElem.send_keys('PASSWORD') #password
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/form/div/div/div/div[3]/button[2]/div')
linkElem.click() #logs in
time.sleep(10)
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[6]')
linkElem.click() #enters server
time.sleep(1)
linkElem = browser.find_element_by_css_selector('div.containerDefault-1ZnADq:nth-child(4)')
linkElem.click() #is supposed to enter channel but doesn't work

【问题讨论】:

  • 使用相关的 HTML 或一组演示凭据 (EMAIL/PASSWORD) 更新问题
  • 你做对了兄弟,如果你想选择一个频道,那么你不需要用 CSS 或 Xpath 选择频道,只需使用频道的 URL,如 browser.get('@ 987654321@ url') 如果你没有得到它,只需回复我会告诉你我的代码

标签: python html css python-3.x selenium


【解决方案1】:

我尝试点击的频道项目的类名是“wrapper-1BJsBx”,标签类型是“a”。 也许“1BJsBx”是一个自动生成的后缀,所以我建议使用正则表达式来避免大量维护。 我可以使用以下命令点击特定频道:

for elem in browser.find_elements_by_css_selector("a[class^=wrapper-]")
  if "my_awesome_channel_name" in elem.get_attribute('aria-label').lower():
    elem.click()
    break

使用 python3.7 和 selenium 3.141.0

【讨论】:

  • 这似乎对我有用。它不会引发错误,但会与网页交互。 a[class^=wrapper-1ucjTd] 是我用的东西。
【解决方案2】:

css 选择器不正确,进入检查面板,选择元素,右键单击它,选择复制 > 复制选择器并粘贴该选择器

【讨论】:

    【解决方案3】:

    复制这一行的Xpath,你就可以点击它,我很确定

    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
    import random
    PATH = "C:\Program Files (x86)\chromedriver.exe"
    driver = webdriver.Chrome(executable_path=PATH)
    driver.get("https://discord.com/channels/191530268761260034/230742511235104769")
    print(driver.title)
    try:
        sold = WebDriverWait(driver, 5).until(
    
            EC.presence_of_element_located(
                (By.XPATH, '/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[1]/div/div[2]/input'))
        )
    except:
        pass
    textName = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[1]/div/div[2]/input')
    textName.send_keys('Put_Email_Here')
    
    textCode = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[2]/div/input')
    textCode.send_keys('Put_Code_Here')
    
    #confirm button
    confrim = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]').click()
    
    #wating for the server to load
    try:
        sold = WebDriverWait(driver, 5).until(
    
            EC.presence_of_element_located(
                (By.XPATH,
                 '/html/body/div/div[2]/div/div[2]/div/div/div/div[2]/div[1]/nav/div[4]/div/div[5]/div[1]/div/div'))
        )
    except:
        pass
        channel1 = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/div/div[2]/div[1]/nav/div[4]/div/div[5]/div[1]/div/div').click()
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2019-08-22
      • 2020-10-04
      • 2021-11-23
      • 1970-01-01
      • 2020-09-06
      • 2020-08-25
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多