简体   繁体   中英

Write a auto-fill and submit web form program

I am trying to write a program which can automatically fill in and submit a form in a web in particular time slot.

But i have no idea how and where to start. i searched this in google, but only resulting very general answer like using JavaScript, python. Can anyone tell me which languages should i learn first?

Despite the fact that the generic advice on this thread is quite good, it's pretty broad. I have tackled this problem myself, and despite the fact that I posted a fully functional example, it was deleted by a moderator, despite "theoretically answering the questions".

So, for anyone else looking to solve this problem, you will want to do the following: Use Selenium and openpyxl, these are two modules that are relatively straight forward and will perform this task perfectly.

You will use selenium to open your web page, and retrieve the relevant html elements that you wish to populate. I suggest finding elements by xPath if you aren't well versed in HTML. The Xpath finder google chrome addon will make this very easy to do.

The driver.get() and driver.find_element_by_xpath() will be the functions that you need.

We will use openpyxl to work with our excel sheet. 'load_workbook()' will load a workbook. We will then use the 'sheet = workbook.active' function to access a sheet from within the workbook.

We now have the functionality to open our website and select an excel sheet.

Now we need to assign cell values to variables, so that we can then populate the HTML form. We assign a variable to each COLUMN in the workbook. So if column A contained first_names we could assign that to by a variable by writing 'FNAME = sheet['A']. Now that we have a way of referring to cells within columns we can begin feeding the data into our HTML form.

We populate our form by using the .send_keys() function in Selenium.

first_name.send_keys(FNAME.value)

the .value makes sure that the correct value is displayed, as sometimes selenium will print the cell function rather than value, we do not want this to happen.

Now that we can print values to our HTML forms from our excel sheet we will need to iterate through each row. We do this with a simply while loop:

i = 1
x = 1
while x <= 50:
  first_name.send_keys(FNAME[i].value)
  i+=1
  x+=1
driver.quit

Once the loop occurs 50 times, the driver will quit, closing the browser and stopping the script.

Some other misc stuff you may find useful when trying to automate this:

driver.back()

time.sleep()

If you would like to see an actual working example feel free to PM me, as apparently posting it here doesn't contribute to the discussion.

The answers you found, as general as they are, are correct. I'll try to explain it to you.

As you are trying to automatize some activity, you have to use a script language to basically

  1. Get stuff references (like getting indexes, forms/fields IDs, etc)
  2. Insert/change stuff (like filling a field, following the field references)
  3. Save stuff (prepare a file, with field references and it's content, that can be injected to the specific system or website)

One example of script language is Python .

So you already have your script. Now you need to inject it on the page. The programming language that can do it for you is Javascript, or JS . It allows you to talk with the webpage, GETting data/references or POSTing data.

I suggest you to write a Python script using a library called Selenium Webdriver .

It will be easy to implement what you have in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM