Reputation: 99
I am trying to use selenium with python to automate some discord tasks. I have it open browser to discordapp.com, login, go to the server in question, but I can't find any css selector, xpath, class name, etc to be able to click on a channel called "commands". Any help would be appreciated.
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
Upvotes: 2
Views: 6577
Reputation: 21
Copy the Xpath of this line and you'll be able to click it im pretty sure
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()
Upvotes: 1
Reputation: 29
The css selector is incorrect, go into the inspect panel, select the element, right click on it, select copy > copy selector and paste that selector
Upvotes: 1
Reputation: 154
The channels items I tried to click on had a class name of "wrapper-1BJsBx" and a tag type of "a". Maybe "1BJsBx" is an autogenerated suffix so I propose using regex to avoid lots of maintenance. I could click on specific channel using the following:
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
using python3.7 and selenium 3.141.0
Upvotes: 3