首页 > 解决方案 > 如何创建脚本来自动填充字段?

问题描述

我想用 JavaScript 创建一个脚本,让我可以自动访问一个网站并自动填写该网站上的字段。自动填写所有字段后,我希望单击提交按钮并重新开始该过程。我通过它们的唯一 ID 定义字段。我现在遇到的问题是,当我运行脚本时,我没有自动访问网站。谁能帮助我如何改进我的脚本,以便我可以自动化这个过程?

这是我的代码:

function goingToWebsite(){
        window.location.href = 'https://example.com/';
    }

    function fillForm(){
    document.getElementById('gender').value = "Herr"
    document.getElementById('lastname').value = "Example";
    document.getElementById('firstname').value = "Example";
    document.getElementById('address').value = "Example";
    document.getElementById('postcode').value = "Example";
    document.getElementById('city').value = "Example";
    document.getElementById('mobile').value = "Example";
    document.getElementById('email').value = "Example";
    document.getElementById('country_code').value = "Example";
    document.getElementById('birthday').value = "Example";
    document.getElementById('profession').value = "Example";
    document.getElementById('hobbies').value = "Example";
    document.getElementById('skills').value = "Example";
    document.getElementById('wish').value = "Example";
    element = document.getElementById('agb-checkbox');
    element.checked = true;
    }

    function submitForm(){
        document.getElementsByName("JETZT ANMELDEN").click();
    }

    function main(){
        for (let i=0; i<99; i++){
        goingToWebsite();
        fillForm();
        submitForm();
        setTimeout(timeout, 3000);
        }
    }

非常感谢

我现在有这样的:

const puppeteer = require('puppeteer');
async function main(){
    const browser = await puppeteer.launch({
        headless: false
    });
    for (let i=0; i<3; i++){
    const page = await browser.newPage();
    await page.goto('https://beispiel.de/');
    await page.type('#gender', 'Herr');
    await page.type('#lastname', 'Beispiel');
    await page.type('#firstname', 'Beispiel');
    await page.type('#address', 'Beispiel 29');
    await page.type('#postcode', 'Beispiel');
    await page.type('#city', 'Beispiel');
    await page.type('#mobile', '0041797766666');
    await page.type('#email', 'Beispiel@outlook.com');
    await page.type('#country_code', 'Deutschland');
    await page.type('#birthday', '28.07.1995');
    await page.type('#profession', 'Beispiel');
    await page.type('#hobbies', 'Beispiel');
    await page.type('#skills', 'Beispiel');
    await page.type('#wish', 'Beispiel');
    await (await page.waitForSelector('#agb-checkbox')).click();
    const searchBtn = await page.$x("//button[@class='form-btn']");
    searchBtn[0].click();
    await page.waitForTimeout(5000);
    await browser.close();
    }
}
main();

当第一个循环完成时,它不会再次重新加载新页面,这可能是什么原因?

标签: javascriptdomautomationscripting

解决方案


您不能使用客户端 javascript 执行此操作。您应该为此使用服务器端语言。位置更改后,您无法管理当前站点。如果你熟悉 node js,你可以使用puppeteer


推荐阅读