13

我打算创建一个 Android 应用程序,该应用程序对网站执行无头登录,然后在保持登录会话的同时从后续页面中抓取一些内容。

我首先在一个普通的 Java 项目中使用了HtmlUnit,它工作得很好。但后来发现 HtmlUnit 与 Android 不兼容。

然后我通过向登录表单发送 HTTP“POST”请求来尝试JSoup库。但是由于 JSoup 不支持 JavaScript,因此生成的页面并没有完全加载。

然后有人建议我看看Selendroid,它实际上是一个 android 测试自动化框架。但我真正需要的是一个同时支持 JavaScript 和 Android 的 Html 解析器。我发现 Selendroid 很难理解,我什至无法弄清楚要使用哪些依赖项。

  • selendroid 客户端
  • selendroid-独立
  • selendroid 服务器

使用Selenium WebDriver,代码将像下面这样简单。但是有人也可以给我看一个类似的Selendroid代码示例吗?

    WebDriver driver = new FirefoxDriver();
    driver.get("https://mail.google.com/");

    driver.findElement(By.id("email")).sendKeys(myEmail);
    driver.findElement(By.id("pass")).sendKeys(pass);

    // Click on 'Sign In' button
    driver.findElement(By.id("signIn")).click();

并且,

  1. 要添加到我的 Gradle.Build 文件中的依赖项是什么?
  2. 要导入哪些 Selendroid 库?
4

3 回答 3

1

我从来没有用过Selendroid,所以我不太确定,但是通过网络搜索我找到了这个例子,根据它,我想你的代码从Seleniumto翻译Selendroid是:

翻译代码(在我看来)

public class MobileWebTest {
  private SelendroidLauncher selendroidServer = null;
  private WebDriver driver = null;

  @Test
  public void doTest() {
    
     driver.get("https://mail.google.com/");

     WebElement email = driver.findElement(By.id("email")).sendKeys(myEmail);
     WebElement password = driver.findElement(By.id("pass")).sendKeys(pass);

     WebElement button = driver.findElement(By.id("signIn")).click();

     driver.quit();
  }

  @Before
  public void startSelendroidServer() throws Exception {
    if (selendroidServer != null) {
      selendroidServer.stopSelendroid();
    }

    SelendroidConfiguration config = new SelendroidConfiguration();

    selendroidServer = new SelendroidLauncher(config);
    selendroidServer.launchSelendroid();

    DesiredCapabilities caps = SelendroidCapabilities.android();

    driver = new SelendroidDriver(caps);
  }

  @After
  public void stopSelendroidServer() {
    if (driver != null) {
      driver.quit();
    }
    if (selendroidServer != null) {
      selendroidServer.stopSelendroid();
    }
  }
}

你有什么要添加到你的项目

看来您必须将Selendroid standalone jar file. 如果您对如何在 Android 项目中添加外部 jar 有疑问,可以查看以下问题:如何在 Android 项目中使用外部 JAR?

在这里你可以下载jar file: jar 文件

Also, it seems that it is not enough just to add the jar file to your project. You should add too the selendroid-client jar file of the version of standalone that you have.

You can download it from here: client jar file

I expect it will be helpful for you!

于 2015-08-21T16:18:35.300 回答
1

Unfortunately I didn't get Selendroid to work. But I find a workaround to scrape dynamic content by using just Android's built in WebView with JavaScript enabled.

mWebView = new WebView();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new HtmlHandler(), "HtmlHandler");

mWebView.setWebViewClient(new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
       super.onPageFinished(view, url);

       if (url == urlToLoad) {
       // Pass html source to the HtmlHandler
       WebView.loadUrl("javascript:HtmlHandler.handleHtml(document.documentElement.outerHTML);");

   }
});

The JS method document.documentElement.outerHTML will retrieve the full html contained in the loaded url. Then the retrived html string is sent to handleHtml method in HtmlHandler class.

class HtmlHandler {
        @JavascriptInterface
        @SuppressWarnings("unused")
        public void handleHtml(String html) {
            // scrape the content here

        }
    }

You may use a library like Jsoup to scrape the necessary content from the html String.

于 2015-08-26T18:24:47.477 回答
0

我建议您使用 WebdriverIO,因为您想使用 Javascript。它使用 NodeJs,因此很容易需要其他插件来抓取 HTML。

Appium 也是一种替代方案,但它更侧重于前端测试。

于 2015-08-21T09:15:05.867 回答