Home > Software design >  Accepting a Discord Invite using Selenium
Accepting a Discord Invite using Selenium

Time:07-07

I'm trying to create a simple script to accept a Discord Invite. I've managed to login to an account however I cannot get it to accept the invite.

I've tried using the XPath aswell as using CSS selector to try and find the button but i'm having no luck (examples seen below).

browser.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/div/section/div/button")

browser.find_element_by_class_name('marginTop40-i-78cZ.button-3k0cO7.button-38aScr.lookFilled-1Gx00P.colorBrand-3pXr91.sizeLarge-1vSeWK.fullWidth-1orjjo.grow-q77ONN')

The outer html is:

<button type="button" ><div >Accept Invite</div></button>

Full XPath:

/html/body/div[1]/div[2]/div/div[1]/div/div/div/section/div/button    

I've attached a discord invite link at the bottom of the page as an example.

Any help would be much appreciated.

https://discord.com/invite/the1

CodePudding user response:

The desired element is a dynamic element, so to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='button'] > div[class^='contents']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button/div[starts-with(@class, 'contents') and text()='Accept Invite']"))).click()
    
  • Note: You have to add the following imports :

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