When i send string with send_keys() its taking substring in chrome driver (selenium python)



  • Hi all i'am facing problem with selenium (i'am using python) for chrome driver, when i send some string with .sendkeys( ) some time its taking first two characters and some time its taking first character and some times it's taking entire word.
    I'am not understanding why its taking like that. Please help me on this.

    Code:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id=
    'dashBoradContainer']/kl-wdg-create/div/div[2]/div/div[2]/kl-basic-chart-config
    /div/div[2]/div[1]/div/div[1]/div/div[1]/input"))).send_keys("sel_report")
    


  • I've had this issue with a website previously that was doing validation after each keystroke. Sometimes I could get 5 characters in before it would validate, sometimes only 2. In the end, although it might not be the right answer, I found that if I converted the string to a byte array, and entered the characters in one at a time in a foreach loop with a 5-10 ms wait (cringe, I know) after each character, the strings started being entered correctly every time. ex:

        nameString = "sel_report"
        nameBytes = bytes(nameString, 'utf-8')
        xpathString = "//*[@id=
        'dashBoradContainer']/kl-wdg-create/div/div[2]/div/div[2]/kl-basic-chart-config
        /div/div[2]/div[1]/div/div[1]/div/div[1]/input"
        elem = driver.find_element_by_name(xpathString)
        for b in nameBytes:
          selem.send_keys(b)
          time.sleep(0.005)
    

    Sorry about the horrible python, I really don't use it often. I hope that there's a better answer, but, as I said, this has worked for me in the past.

    Also, as per PDHide's comment, you should probably find a better wait condition unless you're really just waiting for it to be clickable, which, even then, I'm sure that a better one would probably exist in your context.



Suggested Topics