【问题标题】:Run line of javascript code over multiple pages在多个页面上运行一行 javascript 代码
【发布时间】:2014-02-24 21:39:18
【问题描述】:

我想运行一行简单的代码:

document.getElementById('..').click();document.getElementById('..').click();

在同一网站的多个页面上。我有所有这些页面的列表,我想打开页面,单击这两个元素并关闭页面超过 700 次。我可以对 chromium 控制台进行编程来做到这一点(考虑到加载时间)吗?

【问题讨论】:

  • 也许值得查找 Selenium(一种测试工具),它应该能够为您轻松完成此操作。看这里-docs.seleniumhq.org

标签: javascript google-chrome curl


【解决方案1】:

您可以使用PhantomJS 完成此任务。

Here is an article with very detailed explanations。也许看看 PhantomJS 页面上的例子就足够了。

【讨论】:

  • 感谢您的提示,我稍后会仔细查看,乍一看它很大,将变得有用。现在我在 chromium 上安装了简单的宏记录器,它可以解决问题。
【解决方案2】:

你可以做这样的事情(它应该可以在几乎任何浏览器的控制台上工作)

首先,您需要位于您的网页所属的同一网站上,并且您还应该允许为该网站显示弹出窗口。

const pages = ['https://example.com/page_1', 'https://example.com/page_2', ...]; //Your list of page links (relative or absolute)

async function doAutomatedStuff(links) { //Use 'async' to make use of 'await' later
    for (const link of links) {
        let page = window.open(link, '', "location=no, toolbar=0, width=600, height=600"); //Must be executed from the same website and pop-ups are allowed
        page.onload = function (event) { //Executed after the whole page is loaded
            page.document.getElementById('..').click();
            //If your click() event does something async, you may want to wait for it first before closing the page
            page.close();
        };
        await new Promise(r => setTimeout(r, 2000)); //Wait and block (delay) for a reasonable amount of time before proceeding to the next link
    }
}

doAutomatedStuff(pages); //Execute

请记住,您需要在处理一个链接和下一个链接之间设置一些延迟,否则您会收到太多弹出窗口并且浏览器可能会崩溃。

另外,浏览器会限制每个网站打开弹窗的数量,我认为这可以在浏览器的配置中更改,但我建议您在完成后重新设置。

在匿名或隐身模式下使用以避免混乱您的历史记录

【讨论】:

    猜你喜欢
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多