Selenium 仅用于测试?

Selenium only for testing?

我刚开始为 android 开发应用程序。我写了一些代码,登录到一个网站(使用大量 JavaScript)并获取一些我需要的数据。我用于那个 HtmlUnit。现在我明白了,HtmlUnit 在 android 上不起作用,所以我需要一个替代方案。我做了这样的事情...

final WebClient webClient = new WebClient(BrowserVersion.EDGE);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    final HtmlPage loginSite = webClient.getPage("...");

    final HtmlForm login_form = (HtmlForm) loginSite.getElementById("login_form");

    final HtmlSubmitInput button = (HtmlSubmitInput) login_form.getByXPath("//input[@id='login_submit_button']").get(0);

    final HtmlTextInput id = login_form.getInputByName("user");
    id.setValueAttribute("...");

    final HtmlPasswordInput pw = login_form.getInputByName("password");
    pw.setValueAttribute("...");

    button.click();

    webClient.waitForBackgroundJavaScript(2000);

    HtmlPage worldSelectionForm = (HtmlPage) webClient.getTopLevelWindows().get(0).getEnclosedPage();

    final List<HtmlAnchor> anchorlist = worldSelectionForm.getAnchors();

    getWorld(anchorlist, 126).click();
    webClient.waitForBackgroundJavaScript(5000);

    HtmlPage game = (HtmlPage) webClient.getTopLevelWindows().get(0).getEnclosedPage();
    System.out.print(game.asXml());


 public static HtmlAnchor getWorld(List<HtmlAnchor> anchorlist, int world) {
        HtmlAnchor anchor = null;
        for (HtmlAnchor a : anchorlist) {
            if (a.toString().contains("server_de" + String.valueOf(world))) {
                anchor = a;
            }
        }
        return anchor;
    }

我搜索了很多,但没有找到问题的答案。

我也可以用 Selenium 做这个吗?硒仅用于测试吗?

问候

对于“Selenium 仅用于测试吗?”这个问题,Selenium site 开头为:

Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

查看 Selendroid, which replaces the Android Driver

他们的 Quick Start 页面上有一些很好的例子。

Selenium (WebDriver) 的关键决定不是你是否 "testing",而是你是否需要 interactivity:你是否需要 模拟一个人使用真实浏览器。

如果您需要交互性,那么众多 WebDriver 实现(例如 Selendroid)中的一种将是您所需要的。

然而,如果你不这样做,如果你只是需要从网站上抓取内容,那么就使用 HTTP 库。这将更快、更有弹性,并将避免浏览器或设备特定的问题。它们可以处理 cookie、影响登录,并拥有 HTTP 的所有功能,但没有浏览器的重量,它们比交互式工具有很大的优势。

例如,您可以使用 JSoup(Android 示例)非常有效地抓取和解析内容。