2

Is there something faster I can use to fill forms since finding id's and sending info via .send_keys takes like 10 seconds for 7 fields. I'm using this currently but its way to slow for what I need it to do, thanks for any help.

driver.find_element_by_xpath("//*[@id='order_billing_name']").send_keys("John Doe")
driver.find_element_by_xpath("//*[@id='order_email']").send_keys("supreme@gmail.com")
driver.find_element_by_xpath("//*[@id='order_tel']").send_keys("012-345-6789")
driver.find_element_by_xpath("//*[@id='bo']").send_keys("439 N Fairfax Ave")
driver.find_element_by_xpath("//*[@id='order_billing_city']").send_keys("Los Angeles")
driver.find_element_by_xpath("//*[@id='order_billing_zip']").send_keys("90036")
driver.find_element_by_xpath("//*[@id='nnaerb']").send_keys("1111222233334444")
JeffC
  • 22,180
  • 5
  • 32
  • 55
Joe Cook
  • 59
  • 1
  • 8
  • Is the delay in sending the keys, or in finding the elements? Try temporarily removing the `.send_keys(...)` from these calls and see if that makes a noticeable difference. – John Gordon May 06 '18 at 05:29
  • @JohnGordon how can I test that? I'm able to grab item I need and size in under 2 seconds when finding elements however the checkout page just takes long. Even when sendkey is typing it's very slow – Joe Cook May 06 '18 at 06:21

2 Answers2

3

I had this problem a few hours ago. Instead of send_keys() use

driver.execute_script("document.getElementById('idName').setAttribute('value','text_to_put');
JeffC
  • 22,180
  • 5
  • 32
  • 55
Shahriyar Shawon
  • 334
  • 1
  • 2
  • 9
  • Thank you, it worked perfect and very fast. I tried to do the same method for drop down menu select but that didn't work as well haha. Any suggestions? – Joe Cook May 06 '18 at 09:06
  • I'm not sure but I would assume you would click it first then try to `.click` the right option, have not tried it though – Shahriyar Shawon May 06 '18 at 11:41
  • Just to clarify - this is for finding an element faster, not for making send_keys faster (when much input is used). Also, does this method support XPATH finding? – Roel Van de Paar Jun 06 '19 at 10:07
0

To fill the form fields quicker you can use induce WebDriverWait for the element_to_be_clickable() and then use execute_script() as follows:

  • Using CSS_SELECTOR:

    text_to_insert = "John Doe"
    driver.execute_script("arguments[0].value = '" + text_to_insert + "';", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#order_billing_name"))))
    
  • Using XPATH:

    text_to_insert = "John Doe"
    driver.execute_script("arguments[0].value = '" + text_to_insert + "';", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='order_billing_name']"))))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352