Fast writing in a textBox with selenium and python

前端 未结 1 1661
轮回少年
轮回少年 2021-01-26 13:17

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

<
1条回答
  •  没有蜡笔的小新
    2021-01-26 13:45

    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)
    

    0 讨论(0)
提交回复
热议问题