0

I'm using Selenium with Python (Chorme driver) to write something in a text box but there are a lot of textboxes and I need it to fill them faster. I use a sequences of

driver.find_element_by_xpath("//input[@class='string required' and @id='order_billing_name']").send_keys("test.com")

commands but to write 10-11 these takes a lot of time. Is there a way to fast it up?

6
  • What exactly do you mean by fill them faster? What is your exact usecase? Commented Sep 14, 2018 at 17:05
  • because after a period of 12 secon the page will reload , so i need to fill the textbox faster Commented Sep 14, 2018 at 17:09
  • try finding all the elements at once based on just the class: elements = driver.find_elements_by_xpath("//input[@class='string required']"). Then loop: for e in elements: sendkeys... Commented Sep 14, 2018 at 17:19
  • How many fields do you want to fill up in 12 seconds? Is it manually possible? What does the usecase says about it? Commented Sep 14, 2018 at 17:22
  • first thing is to find out what's taking long time: find_element_by_xpath or send_keys. Different problems. Also since your xpath includes ID, there's really no need for other atributes, so your xpath could be "//input[@id='order_billing_name']". But then if it's always ID, you could use find_element_by_id instead, which is faster. Although still, figure out first what takes time: finding or typing Commented Sep 14, 2018 at 17:49

1 Answer 1

1

You can try to set values using javascript:

orderBillingName = driver.find_element_by_id("order_billing_name")
driver.execute_script("arguments[0].value=arguments[1];", orderBillingName, "mytext")

You can set all directly with javascript:

driver.execute_script("document.querySelector('#order_billing_name').value='mytext';"\
                   "document.querySelector('.order_number_class').value='12323';"\
                   "document.querySelector('input#anotherinputid').value='anything';")

Example for a lot fields:

fields = {
    "input#order_billing_name": "some text", 
    ".order_number_class"     : "some text", 
    "css selector"            : "some text", 
    "css selector"            : "some text",
    "css selector"            : "some text"
    }

js = ""
for selector, value in fields.items():
    js = js + "document.querySelector('" + selector + "').value='"+ value +"';"

driver.execute_script(js)
Sign up to request clarification or add additional context in comments.

8 Comments

the problem is that doing river.find_element_by_id("order_billing_name") it can't find the textbox for this reason i did that with xpath
@Marià css selector input#order_billing_name or input[id='order_billing_name'] should work
it seems to work but if i want to "document.querySelector('.order_number_class').value='12323';" but inste 12323 i want ssend a python variable ?
@Marià of cause you can send variable. For example check how you can handle your all fields in my answer update.
@Marià I didn't understand what "javascript code like for instance an alert" mean. With execute_scriptyou can send any javascript code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.