简体   繁体   English

如何通过WWW :: Mechanize :: Firefox调用jQuery或Angular函数?

[英]How can I call jQuery or Angular functions via WWW::Mechanize::Firefox?

I'm trying to execute jQuery/Angular calls via WWW::Mechanize::Firefox's eval_in_page . 我正在尝试通过WWW :: Mechanize :: Firefox的eval_in_page执行jQuery / Angular调用。 I'm guessing there is some sort of scope issue, because I'm getting errors. 我猜是存在某种范围问题,因为我遇到了错误。

If I try to execute this code: 如果我尝试执行此代码:

angular.element(document.getElementsByClassName("input")[0]).triggerHandler(('change');

I get this response: 我收到了这个回复:

MozRepl::RemoteObject: ReferenceError: angular is not defined at ... MozRepl :: RemoteObject:ReferenceError:angular未定义为...

If I try to execute this code: 如果我尝试执行此代码:

$(".input").val("Foo")

I get this response: 我收到了这个回复:

MozRepl::RemoteObject: ReferenceError: $ is not defined ... MozRepl :: RemoteObject:ReferenceError:$未定义...

It seems I don't have access to either framework. 看来我无法访问任何一个框架。 The page does appear to have both loaded, however. 但是,该页面似乎都已加载。 If I execute the code in the browser console it works fine. 如果我在浏览器控制台中执行代码,它可以正常工作。

You could click inside the field with $mech->click() , it accepts x, y coordinates, insert the text and remove the focus again. 您可以使用$mech->click()在字段内$mech->click() ,它接受x, y坐标,插入文本并再次移除焦点。 I used PhantomJS for similar work before. 之前我使用过PhantomJS进行类似的工作。 It should work for WWW::Mechanize::Firefox too. 它也适用于WWW::Mechanize::Firefox

use WWW::Mechanize::PhantomJS;
use strict;
use warnings;

my $mech = WWW::Mechanize::PhantomJS->new(log => 1);
$mech->get('http://example.com');

if($mech->success) { 
   # accepts x, y coordinates
   $mech->click(.. field[2]);

   # set a text for example in input-field number 2
   $mech->eval('document.getElementsByTagName("input")[2].value = "Your value";');   

   # Click somewhere else to trigger blur event
   $mech->click(.. outside the field[2]);

  # Read the converted textual input from span
}

Thanks for the ideas all. 感谢所有的想法。 I wasn't able to solve the original question, but I was able to come up with this alternate solution. 我无法解决原来的问题,但我能够提出这个替代解决方案。 Using this javascript I was able to fill in the field and trigger the appropriate change and blur events. 使用这个javascript我能够填写该字段并触发相应的更改和模糊事件。 Doing this allowed angular to do the processing it needed to do. 这样做允许角度进行它需要做的处理。

$js = qq(
  var myElement = document.getElementsByClassName("input")[0];
  myElement.value = "$value";

  function fireEvent(element, event) {
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent(event, true, true); // event type,bubbling,cancelable
    return !element.dispatchEvent(evt);
  }

  fireEvent(myElement, 'change');
  fireEvent(myElement, 'blur');
);
$mech->eval_in_page($js);

Note the function I used was a trimmed down solution found here . 请注意,我使用的功能是在此处找到的精简解决方案。 I trimmed it down to just the Firefox code. 我把它修剪成Firefox代码。 If you're using a different browser, you'd want to use their more robust code. 如果您使用的是其他浏览器,则需要使用更强大的代码。 If anyone comes up with a solution to the original question I'd be happy to accept it. 如果有人想出原始问题的解决方案,我会很乐意接受它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM