rzlvmp
rzlvmp

Reputation: 9442

Can't send some keys (send_keys) in selenium chrome driver

I trying to insert some text in login text box using selenium and python (Chrome driver).

At first I created driver →

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium import webdriver

options = Options()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('--window-size=1200,1024')
drv = webdriver.Chrome(driver_type, options=options)

After that I tried to insert text →

# first try
WebDriverWait(drv, 3).until(EC.presence_of_element_located((By.ID, 'email'))).send_keys('ageb')

# second try
login = 'ageb'
WebDriverWait(drv, 3).until(EC.presence_of_element_located((By.ID, 'email'))).send_keys(login)

# third try
drv.find_element_by_id("email").send_keys('ageb')

# fourth try
for chr in 'ageb':
  drv.find_element_by_id("email").send_keys(chr)

# fifth try
elem = drv.find_element_by_id("email")
for character in login_user:
    actions = webdriver.ActionChains(drv)
    actions.move_to_element(elem)
    actions.click()
    actions.send_keys(character)
    print(character)
    actions.perform()
    sleep(1)

In all cases e working as backspace

Input string Result string that I can see in browser
ageb ab
aggeb agb
ageeb b

In my local MacOS script working perfect, but problem appear at development Ubuntu 18.04 Server.
I lost hope of finding a solution.

PS-1 clipboard don't working properly too (because of no GUI on server). I tried to install xclip without no luck. Anyway I don't want to use this workaround.
PS-2 javascript (execute_script()) also can't be used because front end source was built at deploy server and I don't have access to reactive setters directly from browser

Looks like here is keyboard layout problems. I trying to run Chrome via SSH session with X forwarding (but headless Chrome behavior is same).

Infrastructure specs

ChromeDriver 90.0.4430.24

Google Chrome 90.0.4430.85

/etc/default/keyboard →
    XKBMODEL="pc105"
    XKBLAYOUT="jp"
    XKBVARIANT=""
    XKBOPTIONS=""
    BACKSPACE="guess"

Linux 4.15.0-54-generic #58-Ubuntu x86_64 GNU/Linux

Ubuntu 18.04.2 LTS (Bionic Beaver)

Python 3.6.9

selenium 3.141.0

Upvotes: 3

Views: 1121

Answers (1)

Prophet
Prophet

Reputation: 33371

Not sure, but you can try sending the text with Actions instead of webdriver.
Like this:

elem = driver.find_element_by_id("email")
text = "Your text to be typed"
for character in text:
  actions = ActionChains(driver)
  actions.move_to_element(elem)
  actions.click()
  actions.send_keys(character)
  print(character)
  actions.perform()
  time.sleep(random.uniform(0.2,0.5))

Also this maybe will be usable. It's in Java but I think it can be easily converted to Python

public void sendTextAction(By locator, String text){
    action = new Actions(driver);
    clickVisible(locator);
    action.sendKeys(text).build().perform();
    action.sendKeys(Keys.ENTER).build().perform();
}

Upvotes: 1

Related Questions