3

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
Cedric
  • 31
  • 5
  • 1
    Try with text = “hello”+”’”+ “World”. Sometime this issue appears because of keyboard layout. – Muzzamil Jan 30 '20 at 21:01

2 Answers2

2

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:

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Could you explain how the clickability of the element makes the difference between `helloworld` and `hello'world`? – Steven Rumbalski Jan 30 '20 at 21:00
  • 1
    @StevenRumbalski In short, _Google Search Home Page_ elements are [JavaScript](https://www.javascript.com/) enabled element. Inducing _WebDriverWait_ for _element_to_be_clickable()_ we need to ensure that the element is _clickable_ i.e. all the _JavaScript_ and _AJAX Calls_ are completed, so the character sequence sent through `send_keys()` doesn't get distorted. – undetected Selenium Jan 30 '20 at 21:06
  • This doesn't seem to fix the problem, could it have to do with my keyboard layout? (azerty, Belgium) Although the ' seems to be the only character that doesn't get through. – Cedric Jan 30 '20 at 21:16
  • @Cedric Good point, I need some time to check if any of the _keyboard layout_ have any impact on `send_keys()` method anyway. – undetected Selenium Jan 30 '20 at 21:21
  • 1
    @Cedric Checkout the answer update and let me know the status. – undetected Selenium Jan 30 '20 at 21:45
  • Changing my keyboard layout isn't really an option. The commit that presumably fixed the issue is for Java, but i'm using Python. "éèe" Is sent correctly, The only faulty character I found is ' and ". – Cedric Jan 31 '20 at 10:53
  • 1
    @Cedric Possibly, you are right, I wish I could have reproduced the issue at my end to dig further. In-case of any positive development I will keep you updated. – undetected Selenium Jan 31 '20 at 10:57
0

Solution: Changed from Chromedriver to Firefox using geckodriver. The single and double quote seems to ge glitched in the current version of Chromedriver.

Cedric
  • 31
  • 5