Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium send_keys doesn't sent apostrophe

In Selenium I want to input a teststring "hello'world", but the webpage's textbox becomes "helloworld". As if the apostrophe doesn't exist. Replacing "'" by chr(39) or splitsing the string doesn't do the job either.

  • My part of the code: (using Chrome webdriver in python)
driver = webdriver.Chrome()
driver.get("https://google.com")
text = "hello'world"
textbox = driver.find_element_by_xpath('//* 
[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input')
for i in text:
    textbox.send_keys(i)
    sleep(0.1)
  • Browser screenshot: Browser screenshot
like image 406
Cedric Avatar asked Mar 01 '26 10:03

Cedric


1 Answers

To send the character sequence hello'world within the search box of Google Home Page you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using CSS_SELECTOR:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://google.com")
    text = "hello'world"
    textbox = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
    for i in text:
        textbox.send_keys(i)
    
  • Browser Snapshot:

apostrophe


Update

Seems previously there were some issues with the non-US keyboard settings and Unicode characters while invoking send_keys(). You can find a couple of relevant discussions in:

  • SendKeys does not work correctly with non-US keyboard settings
  • send_keys("é") to IE11 element sends e instead of é
  • 3.5.0 Grid - Selenium Keys / Unicode Failing

This issue was solved through the commit Fixing encoding of payload passed by hub to a node.

Using Selenium v3.5.3 should solve this issue.


tl; dr

Change your keyboard layout

like image 153
undetected Selenium Avatar answered Mar 03 '26 23:03

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!