[英]Accessing anchor with href/Javascript
Testing CasperJS on a system that has an anchor with an href attached to javascript. 在具有锚定并在JavaScript上附加了href的系统上测试CasperJS。 Doing the usual
照常做
casper.then(function() {
this.evaluate(function() {
// document.querySelector('a[id="CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH"]').click(); // works in FF
// window.onload=submitAction_win0(document.win0,'CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH');
//eval("submitAction_win0(document.win0,'CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH_1');");
//submitAction_win0(document.win0,'CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH');
// window.onload=submitAction_win0(document.win0,'CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH');
// this.click('a[id="CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH"]');
// click('a[id="CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH"]');
// self.click('a[id="CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH"]');
// this.this.click('a[id="CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH"]');
..
});
});
None of the above approaches worked. 上述方法均无效。 The Git repos for the projects indicate that Casper has a CasperUtils/ClientUtils lib that appears to have a
__utils__
that has a click method which is the way to invoke href="javascript:foo()"
elements. 项目的Git仓库表明,Casper有一个CasperUtils / ClientUtils库,该库似乎具有
__utils__
,该库具有click方法,该方法是调用href="javascript:foo()"
元素的方式。
However, I can't seem to figure out how to get this running. 但是,我似乎无法弄清楚该如何运行。
If anyone has used CasperJS, and has a sample of code on how this is implemented, we'd be thankful! 如果有人使用过CasperJS,并有示例代码说明了如何实现,我们将不胜感激!
Ideally, the end result should be something like: 理想情况下,最终结果应为:
casper.then(function() {
e=document.querySelector("id['foo']");
CasperUtilsSomething.click(e);
});
Or, if you have a pointer to a test code case that I could run, that would let us see how this is supposed to be implemented. 或者,如果您有一个指向我可以运行的测试代码用例的指针,那将使我们看到应该如何实现。
It looks like the issue here is a confusion around the scope of the function. 看起来这里的问题是关于函数范围的混淆。 This is the tricky thing about working with PhantomJS (which CasperJS is built on top of) - there are two entirely separate scopes, one of which runs in the PhantomJS context and has access to the
casper
object, and the "sandbox" scope that remote code runs in - anything run in casper.evaluate()
, for example, is run in a sandbox in the remote context, with no access to the casper
object or its methods. 这是使用PhantomJS(CasperJS建立在其之上)的棘手事情-有两个完全独立的作用域,其中一个在PhantomJS上下文中运行并可以访问
casper
对象,而“ sandbox”作用域则是远程的代码在其中运行-例如,在casper.evaluate()
中运行的所有内容都在远程上下文中的沙箱中运行,而无权访问casper
对象或其方法。
So trying to call this.click()
within casper.evaluate()
is going to fail - the function you're running has no access to the casper
instance, and this
will refer to the window
object in the browser. 所以,试图调用
this.click()
内casper.evaluate()
是要失败的-你正在运行的功能,具有对没有接入casper
实例, this
将涉及到window
对象在浏览器中。
Usually, the way you'd do this is just: 通常,您执行此操作的方式仅是:
casper.then(function() {
casper.click('#CLASS_SRCH_WRK2_SSR_PB_CLASS_SRCH');
casper.evaluate(function() {
// some function that needs to run after clicking
});
});
Note also that as far as I know, you generally can't run any window.onload
code in casper.evaluate()
- the window's load
event will already have fired by the time you are running your code. 还请注意,据我所知,您通常无法在
casper.evaluate()
运行任何window.onload
代码-运行代码时,窗口的load
事件已经触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.