Python Selenium can't fill input field

Question:

I want to login on a site (https://gmail.hu) with selenium, there is a username input field with ID _user and password input field with ID _pass

The password field is a bit tricky because there is two password field you can see if you open the inspector, one is hidden and the other is not.

<input class="billboard_input" type="password" value="" name="_pass" id="_pass" maxlength="16" style="display: block;"> 

<input class="billboard_input" type="text" value="Jelszó" name="_pass_dummy" id="_pass_dummy" style="display: none;">

I tried to get the element by ID but I can’t get it working:

user = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "_user")))
pw = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "_pass")))

By XPATH I can fill the username input but I can’t figure out how can I fill the password input, I tried to send_keys to both password input but nothing happening:

url = 'https://gmail.hu'
driver.get(url)

user = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[1]/div[1]/form/input[6]")))
pw = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[1]/div[1]/form/div[1]/input[1]")))
pw2 = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[1]/div[1]/form/div[1]/input[2]")))
                
user.send_keys("test")
time.sleep(5)
pw.send_keys("testpw")
time.sleep(5)
pw2.send_keys("testpw")
Asked By: blyyzz

||

Answers:

Here is one way to do it:

## your imports[...]
import time as t
## browser definition


wait = WebDriverWait(driver, 20)
url = "https://gmail.hu/"
driver.get(url)

cookie_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="ELFOGADOM"]')))
cookie_button.click()
print('accepted cookies')
id_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[id="_user"]')))
id_field.send_keys('viktor_orban')
dummy_password_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[id="_pass_dummy"]')))
dummy_password_field.click()
t.sleep(1)
real_pass_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[id="_pass"]')))
real_pass_field.send_keys('oh well...')
print('sent user & pass')

Result printed in terminal:

accepted cookies
sent user & pass

Selenium documentation can be found at https://www.selenium.dev/documentation/

Answered By: Barry the Platipus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.