html 执行java脚本,HtmlUnit执行自定javascript脚本并回调java方法

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import org.openqa.selenium.support.ui.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.slf4j.LoggerFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;

import com.gargoylesoftware.htmlunit.WebWindow;

import ch.qos.logback.classic.Level;

import ch.qos.logback.classic.LoggerContext;

import net.sourceforge.htmlunit.corejs.javascript.Context;

import net.sourceforge.htmlunit.corejs.javascript.Scriptable;

import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;

import net.sourceforge.htmlunit.corejs.javascript.annotations.JSConstructor;

import net.sourceforge.htmlunit.corejs.javascript.annotations.JSFunction;

import net.sourceforge.htmlunit.corejs.javascript.annotations.JSGetter;

public class TestHtmlUnitDriver {

public static void main(String[] args) {

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME, true);

// WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

loggerContext.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.valueOf("ERROR"));

((HtmlUnitDriver) driver).setJavascriptEnabled(true);

try {

driver.get("https://www.baidu.com");

(new WebDriverWait(driver, 10, 500)).until(new ExpectedCondition() {

public Boolean apply(WebDriver d) {

Context ctx = Context.getCurrentContext();

//Scriptable scope = ctx.initStandardObjects();

try {

WebWindow window = (WebWindow) ReflectUtil.invokeMethod(d, "getCurrentWindow");

Scriptable scope = window.getScriptableObject();

ScriptableObject.defineClass(scope, Counter.class);

} catch (Exception e) {

e.printStackTrace();

}

//Scriptable testCounter = ctx.newObject(scope, "Counter");

System.out.println(((JavascriptExecutor) driver).executeScript("return new Counter(123312).getCount()"));;

Object element = ((JavascriptExecutor) driver)

.executeScript("return document.getElementsByTagName(\"html\")");

System.out.println(element);

return element != null;

}

});

} catch (Exception e) {

e.printStackTrace();

}

driver.quit();

// JavascriptExecutor javascript = (JavascriptExecutor) driver;

// Object obj = javascript.executeScript("return

// document.getElementsByTagName(\"html\")");

// Object obj =

// ((HtmlUnitDriver)driver).executeScript("document.getElementsByTagName(\"html\")");

// System.out.println(obj);

}

public static class Counter extends ScriptableObject {

private static final long serialVersionUID = 438270592527335642L;

// The zero-argument constructor used by Rhino runtime to create instances

public Counter() {

}

// @JSConstructor annotation defines the JavaScript constructor

@JSConstructor

public Counter(int a) {

count = a;

}

// The class name is defined by the getClassName method

@Override

public String getClassName() {

return "Counter";

}

// The method getCount defines the count property.

@JSGetter

@JSFunction

public int getCount() {

return count++;

}

// Methods can be defined the @JSFunction annotation.

// Here we define resetCount for JavaScript.

@JSFunction

public void resetCount() {

count = 0;

}

private int count;

}

}

参考:

https://github.com/tntim96/htmlunit-rhino-fork/blob/master/examples/CounterTest.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值