How to select Discord dropdown option using selenium python

86 Views Asked by At

I have just started working with Selenium and practicing different automations using it. I am attempting to type "/bump" in the chat and select the first option that appears. I have tried using different solutions, such as "By.id" and "By.xpath," but so far, none of them have worked for me.

This is how it looks like.

enter image description here

Here is my current working Python code:

url = "<channel url>"

options = webdriver.ChromeOptions()

options.add_argument(r"--user-data-dir=C:\Users\Fahad\AppData\Local\Google\Chrome\User Data")
options.add_argument(r'--profile-directory=Profile 3')
driver = webdriver.Chrome(chrome_options=options)

# Open the website in the browser
driver.get(url)
time.sleep(10)
actions = ActionChains(driver)
input_ele = driver.find_element(By.XPATH, r'//div[@role="textbox"]').send_keys("/bump")
time.sleep(5)

Now this was the last solution I was using for selecting that option but its not working either.

# Move the mouse to a specific pixel (e.g. x=100, y=200)
actions = ActionChains(driver)
actions.move_by_offset(585, 602)

# Click the mouse at the current position
actions.click()
actions.perform()
time.sleep(5)

How to accomplish this task?

class names and HTML code structure of the Discord dropdown:

  <div role="listbox" class="listItems-6eZzQ1" style="inset: 0px 8px;"><div class="clickable-2V8YKY itemWrapper-CeTtIp autocompleteRowVertical-EA9dCg autocompleteRow-14iwvH" role="option" data-list-item-id="channel-autocomplete___0" tabindex="-1" id="autocomplete-0" aria-disabled="false" aria-selected="true"><div class="base-2v-uc0"><div class="wrapper-1IOU8a"><div channel="[object Object]" class="wrapper-3t15Cn image-1a_IXB"><svg width="32" height="32" class="svg-lIB-lq mask-3pLFcz" viewBox="0 0 32 32"><foreignObject x="0" y="0" width="32" height="32" overflow="visible" mask="url(#svg-mask-avatar-default)"><img alt="" class="icon-28xrhD" src="https://cdn.discordapp.com/avatars/302050872383242240/67342a774a9f2d20d62bfc8553bb98e0.webp?size=32" style="width: 32px; height: 32px;"></foreignObject></svg></div><div class="infoWrapper-33i2RS"><div class="usageWrapper-2eaALZ"><div class="text-md-normal-2rFCH3 title-158kHv autocompleteRowHeading-3EcwU6" data-text-variant="text-md/normal" style="color: var(--interactive-active);">/bump</div></div><div class="text-xs-normal-3O7EaX description-1T7FK8 autocompleteRowSubheading-6CMP2P" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">Pushes your server to the top of all your server's tags and the front page</div></div><div class="text-xs-normal-3O7EaX source-3HVB4H autocompleteRowContentSecondary-Oobh2b" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">DISBOARD</div></div></div></div><div class="clickable-2V8YKY itemWrapper-CeTtIp autocompleteRowVertical-EA9dCg autocompleteRow-14iwvH" role="option" data-list-item-id="channel-autocomplete___1" tabindex="-1" id="autocomplete-1" aria-disabled="false" aria-selected="false"><div class="base-2v-uc0"><div class="wrapper-1IOU8a"><div channel="[object Object]" class="wrapper-3t15Cn image-1a_IXB"><svg width="32" height="32" class="svg-lIB-lq mask-3pLFcz" viewBox="0 0 32 32"><foreignObject x="0" y="0" width="32" height="32" overflow="visible" mask="url(#svg-mask-avatar-default)"><img alt="" class="icon-28xrhD" src="https://cdn.discordapp.com/avatars/476259371912003597/dfecd897bfe9a05619470338ea3a3cd4.webp?size=32" style="width: 32px; height: 32px;"></foreignObject></svg></div><div class="infoWrapper-33i2RS"><div class="usageWrapper-2eaALZ"><div class="text-md-normal-2rFCH3 title-158kHv autocompleteRowHeading-3EcwU6" data-text-variant="text-md/normal" style="color: var(--interactive-active);">/bump</div></div><div class="text-xs-normal-3O7EaX description-1T7FK8 autocompleteRowSubheading-6CMP2P" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">Get information on how to bump your server.</div></div><div class="text-xs-normal-3O7EaX source-3HVB4H autocompleteRowContentSecondary-Oobh2b" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">Discord.Me</div></div></div></div></div>

<div role="listbox" class="listItems-6eZzQ1" style="inset: 0px 8px;"><div class="clickable-2V8YKY itemWrapper-CeTtIp autocompleteRowVertical-EA9dCg autocompleteRow-14iwvH" role="option" data-list-item-id="channel-autocomplete___0" tabindex="-1" id="autocomplete-0" aria-disabled="false" aria-selected="true"><div class="base-2v-uc0"><div class="wrapper-1IOU8a"><div channel="[object Object]" class="wrapper-3t15Cn image-1a_IXB"><svg width="32" height="32" class="svg-lIB-lq mask-3pLFcz" viewBox="0 0 32 32"><foreignObject x="0" y="0" width="32" height="32" overflow="visible" mask="url(#svg-mask-avatar-default)"><img alt="" class="icon-28xrhD" src="https://cdn.discordapp.com/avatars/302050872383242240/67342a774a9f2d20d62bfc8553bb98e0.webp?size=32" style="width: 32px; height: 32px;"></foreignObject></svg></div><div class="infoWrapper-33i2RS"><div class="usageWrapper-2eaALZ"><div class="text-md-normal-2rFCH3 title-158kHv autocompleteRowHeading-3EcwU6" data-text-variant="text-md/normal" style="color: var(--interactive-active);">/bump</div></div><div class="text-xs-normal-3O7EaX description-1T7FK8 autocompleteRowSubheading-6CMP2P" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">Pushes your server to the top of all your server's tags and the front page</div></div><div class="text-xs-normal-3O7EaX source-3HVB4H autocompleteRowContentSecondary-Oobh2b" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">DISBOARD</div></div></div></div><div class="clickable-2V8YKY itemWrapper-CeTtIp autocompleteRowVertical-EA9dCg autocompleteRow-14iwvH" role="option" data-list-item-id="channel-autocomplete___1" tabindex="-1" id="autocomplete-1" aria-disabled="false" aria-selected="false"><div class="base-2v-uc0"><div class="wrapper-1IOU8a"><div channel="[object Object]" class="wrapper-3t15Cn image-1a_IXB"><svg width="32" height="32" class="svg-lIB-lq mask-3pLFcz" viewBox="0 0 32 32"><foreignObject x="0" y="0" width="32" height="32" overflow="visible" mask="url(#svg-mask-avatar-default)"><img alt="" class="icon-28xrhD" src="https://cdn.discordapp.com/avatars/476259371912003597/dfecd897bfe9a05619470338ea3a3cd4.webp?size=32" style="width: 32px; height: 32px;"></foreignObject></svg></div><div class="infoWrapper-33i2RS"><div class="usageWrapper-2eaALZ"><div class="text-md-normal-2rFCH3 title-158kHv autocompleteRowHeading-3EcwU6" data-text-variant="text-md/normal" style="color: var(--interactive-active);">/bump</div></div><div class="text-xs-normal-3O7EaX description-1T7FK8 autocompleteRowSubheading-6CMP2P" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">Get information on how to bump your server.</div></div><div class="text-xs-normal-3O7EaX source-3HVB4H autocompleteRowContentSecondary-Oobh2b" data-text-variant="text-xs/normal" style="color: var(--interactive-normal);">Discord.Me</div></div></div></div></div>
1

There are 1 best solutions below

0
Abhay Chaudhary On

I have tried below code with explicit waits and using click and was able to Select the option, I don't have bump option on the server i tested with so I assume the text is /bump but you can correct the locator if needed below

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#driver setup and login steps here for discord

wait = WebDriverWait(driver, 20)

# Wait for the textbox to be visible

textbox = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@role="textbox"]')))

# Enter text in the textbox
textbox.send_keys("/bump")


# Find the "kick" element and click it
kick_element = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@role="listbox"]//div[text()="/bump"]')))
kick_element.click()