Yan Carvalho
Yan Carvalho

Reputation: 33

Use selenium to enter password on website that has virtual keyboard

I am using selenium to log in to a website. But when it arrives on the password page the site asks to enter through a virtual keyboard and the send_keys command does not work.

CODE:

import time
from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys

# Página 1
chrome = wd.Chrome(executable_path='R:\\USUARIOS\\YVieira\\chromedriver.exe')
chrome.get('https://extranet.btgpactual.com')
user=chrome.find_element_by_id('txtLogin')
user.send_keys('yan.vieira')
user.send_keys(Keys.ENTER)
time.sleep(3)
#Página 2

senha=chrome.find_element_by_id('txtSenha')
senha.send_keys("pssw")

HTML CODE:

<input type="password" id="txtSenha" name="txtSenha" maxlength="100">

enter image description here

Upvotes: 1

Views: 1505

Answers (3)

Pranta Mazumder
Pranta Mazumder

Reputation: 11

Click on screen keyboard element.

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

# Replace with your actual driver path
driver = webdriver.Chrome(executable_path="path/to/chromedriver")

# Navigate to the website
driver.get("your_website_url")

# Find and click the password field
password_field = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "password_field_id")) 
)
password_field.click()

# Simulate typing the password using the virtual keyboard
# Assuming the virtual keyboard buttons have specific IDs or classes

# Example: Typing "password"
driver.find_element(By.ID, "virtual_keyboard_button_p").click() 
driver.find_element(By.ID, "virtual_keyboard_button_a").click() 
driver.find_element(By.ID, "virtual_keyboard_button_s").click() 
driver.find_element(By.ID, "virtual_keyboard_button_s").click() 
driver.find_element(By.ID, "virtual_keyboard_button_w").click() 
driver.find_element(By.ID, "virtual_keyboard_button_o").click() 
driver.find_element(By.ID, "virtual_keyboard_button_r").click() 
driver.find_element(By.ID, "virtual_keyboard_button_d").click() 

# Submit the form (if applicable)
submit_button = driver.find_element(By.ID, "submit_button_id")
submit_button.click()

# Close the browser
driver.quit()

Upvotes: 0

Alexey R.
Alexey R.

Reputation: 8676

You can define a function that would go through each letter of your password like (since the virtual keyboard - as per your screen-shot - is a sort of div elements each of which has key attribute having the value corresponding to the key value of the keyboard element)

passwd = "my pass"
for key in passwd:
  chrome.find_element_by_xpath('//div[@key="' + key + '"]').click()

which would just click corresponding divs on the page.

P.S. - this simple example is aassumed to work with simple passwords which do not require shift the key value

Upvotes: 0

Sers
Sers

Reputation: 12255

You can try to use JavaScript to set value:

password = "pssw"
senha=chrome.find_element_by_id('txtSenha')
chrome.execute_script(f"arguments[0].value='{password}'", senha)
chrome.find_element_by_id("btnValidate").click()

Upvotes: 2

Related Questions