python's selenium “send_keys” with chrome driver drops characters

前端 未结 4 1971
青春惊慌失措
青春惊慌失措 2021-01-02 11:59

I am using selenium package with Python (https://pypi.python.org/pypi/selenium) with Windows 7. When I try to login to my facebook account I use the send_keys c

相关标签:
4条回答
  • 2021-01-02 12:02

    Looks like there are (were) some bugs in the Chrome webdriver: https://code.google.com/p/chromedriver/issues/detail?id=435

    The core of the problem looks to be when either the keyboard is configured for a non-English language, or if the webdriver process and the chrome display are running in different language/environments (e.g., when going through a remote display from one host to another, etc.)

    0 讨论(0)
  • 2021-01-02 12:02

    I ran into a similar problem as OP and was satisfied with none of the answers written here nor anywhere else on this site that I could find. Since I read during my research that the issue was at some point in time fixed, I have to assume that it re-appeared nowadays.

    As P.T. mentioned, if you run into similar problems, chances are the chromedrivers/firefoxdrivers are buggy again.

    Since none of the solutions I found helped alleviate the issue, I instead opted for just circumventing selenium and its drivers altogether. So I used javascript to first find the element (through its absolute Xpath), then write to it / set it.

    from selenium import webdriver
    
    def write_to_element(driver, element_xpath, input_string), 
            js_command = f'document.evaluate(\'{xpath}\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value = \'{input_string}\';'
            driver.execute_script(js_command)
    
    driver = webdriver.Chrome()
    driver.get('WebPage/With/Element/You/Want/To/Write/To')
    xpath = 'Xpath/Of/Element/You/Want/To/Write/To'
    write_to_element(driver, xpath, 'SomeRandomInput')
    

    document.evaluate(\'{xpath}\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null) evaluates the xpath to an element, so essentially finds your element on the webpage.

    .singleNodeValue.value = \'{input_string}\';' sets the value of that element to input_string

    0 讨论(0)
  • 2021-01-02 12:21

    Use selenium Ide and export test case in python

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchElementException
    import unittest, time, re
    
    class Test1(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Firefox()
            self.driver.implicitly_wait(30)
            self.base_url = "https://www.facebook.com/"
            self.verificationErrors = []
            self.accept_next_alert = True
    
        def test_1(self):
            driver = self.driver
            driver.get(self.base_url + "/")
            driver.find_element_by_id("email").clear()
            driver.find_element_by_id("email").send_keys("username")
            driver.find_element_by_id("pass").clear()
            driver.find_element_by_id("pass").send_keys("password")
            driver.find_element_by_id("u_0_b").click()
            driver.find_element_by_xpath("//div[@id='u_ps_0_1_5']/div/div").click()
            driver.find_element_by_link_text("1 Requests").click()
            driver.find_element_by_id("globalContainer").click()
    
        def is_element_present(self, how, what):
            try: self.driver.find_element(by=how, value=what)
            except NoSuchElementException, e: return False
            return True
    
        def is_alert_present(self):
            try: self.driver.switch_to_alert()
            except NoAlertPresentException, e: return False
            return True
    
        def close_alert_and_get_its_text(self):
            try:
                alert = self.driver.switch_to_alert()
                alert_text = alert.text
                if self.accept_next_alert:
                    alert.accept()
                else:
                    alert.dismiss()
                return alert_text
            finally: self.accept_next_alert = True
    
        def tearDown(self):
            self.driver.quit()
            self.assertEqual([], self.verificationErrors)
    
    if __name__ == "__main__":
        unittest.main()
    
    0 讨论(0)
  • 2021-01-02 12:29

    I've solved using a custom method for send_keys, which works a little bit lower but fast enough.

    from selenium.webdriver.remote.webelement import WebElement
    
    def send_keys(el: WebElement, keys: str):
        for i in range(len(keys)):
            el.send_keys(keys[i])
    
    send_keys(el, keys)
    
    0 讨论(0)
提交回复
热议问题