pythoncssseleniumselenium-webdriverselenium-chromedriver

Selenium - How to fill a value and click a button


I started today with selenium, and I'm having a problem at finding these elements and filling a value in the first and clicking the second.

<input class="inputDefault-_djjkz input-cIJ7To" name="username" type="text" placeholder="Che nome vuoi usare?" maxlength="999" value="">
<input class="inputDefault-3JxKJ2 input-3ITkQf" type="checkbox" style="width: 24px; height: 24px;">

This is my code

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


# Content to fill - <input class="inputDefault-_djjkz input-cIJ7To" name="username" type="text" placeholder="Che nome vuoi usare?" maxlength="999" value="">
# {"method":"css selector","selector":"[name="Che nome vuoi usare?"]"}

# Checkbox - <input class="inputDefault-3JxKJ2 input-3ITkQf" type="checkbox" style="width: 24px; height: 24px;">

driver = webdriver.Chrome()
driver.get("https://discord.gg/NGvXRfrmUE")
name = driver.find_element_by_class_name("inputDefault-_djjkz input-cIJ7To").send_keys("Test")
button = driver.find_element_by_class_name("inputDefault-3JxKJ2 input-3ITkQf").click

And I get this exception for the first

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".inputDefault-_djjkz input-cIJ7To"}

And this for the second

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".inputDefault-3JxKJ2 input-3ITkQf"}

Solution

  • Since name is available, you should use name attribute.

    driver.find_element_by_name('username').send_keys("Test")
    

    I could see this HTML at my end :

    <input class="inputDefault-_djjkz input-cIJ7To inputField-4g7rSQ" name="email" type="text" placeholder="" aria-label="Email or Phone Number" autocomplete="off" maxlength="999" spellcheck="false" value="">
    

    so here name is email, so one should try :

    driver.find_element_by_name('email').send_keys("Test")
    

    Update 1 :

    driver.get("https://discord.com/invite/NGvXRfrmUE")
    driver.maximize_window()
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys('Test')
    

    Imports :

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