代码之家  ›  专栏  ›  技术社区  ›  Kermit

Python Selenium如何一次填充多个文本框

  •  -1
  • Kermit  · 技术社区  · 8 年前

    我正在使用selenium和python 3.6编写python自动填充脚本。我想尽快填写文本输入框。现在我使用:

    driver.execute_script("document.getElementById(--the elements ID--          
    -).value='%s'" % ---what I want script to fill the box with---)
    

    2 回复  |  直到 8 年前
        1
  •  0
  •   yong    8 年前

    1) 在本地和自动脚本一起准备一个javascript文件,在其中定义一个函数,并接受一个对象,该对象包括要设置的所有字段值,在函数体中查找元素和设置值。

    function autofill(values) {
     document.querySelector("#username").value = values.username;
     document.querySelector("#city").value = values.city;
     ...
    }
    

    2) 使用驱动程序。executeScript()将脚本节点插入头节点,并调用该函数。

    var script = 'var script = document.createElement("script");' +
     'script.src=file:///<script file absolute path>; ' + 
     'document.head.appendChild(script);' +
     'autofill(arguments[0])';
    
    driver.executeScript(script, fieldsValue);
    
        2
  •  0
  •   mostaszewski    8 年前

    您可以尝试这样做:

    elements = driver.find_elements_by_id('your id here')
    for element in elements:
        element.send_keys('---')