简体   繁体   English

模拟单击使用与IE一起使用的Javascript的链接

[英]Emulate clicking a link with Javascript that works with IE

I want to have java script clicking a link on the page..I found something on the net that suggests adding a function like this: 我想让java脚本点击页面上的链接..我在网上发现了一些建议添加如下函数:

function fireEvent(obj,evt){

    var fireOnThis = obj;
    if( document.createEvent ) {
      var evObj = document.createEvent('MouseEvents');
      evObj.initEvent( evt, true, false );
      fireOnThis.dispatchEvent(evObj);
    } else if( document.createEventObject ) {
      fireOnThis.fireEvent('on'+evt);
    }
}

Then call it using: 然后用以下方法调用:

fireEvent(document.getElementById('edit_client_link'),'click');

This seems to work fine for FF but with IE it doesn't work! 这似乎适用于FF,但使用IE它不起作用!

Any ideas? 有任何想法吗?

I think you still need to call document.createEventObject -- you only checked that it's there. 我认为你仍然需要调用document.createEventObject - 你只检查它在那里。 Untested code follows, but based on the docs it should work. 未经测试的代码如下,但基于它应该工作的文档

function fireEvent(obj,evt){

    var fireOnThis = obj;
    if( document.createEvent ) {
      var evObj = document.createEvent('MouseEvents');
      evObj.initEvent( evt, true, false );
      fireOnThis.dispatchEvent( evObj );

    } else if( document.createEventObject ) {
      var evObj = document.createEventObject();
      fireOnThis.fireEvent( 'on' + evt, evObj );
    }
}

This didn't work for me at first and then I saw the code is missing a parameter for the IE portion. 这对我来说不起作用,然后我看到代码缺少IE部分的参数。 Here's an update that should work: 这是一个应该有效的更新:

function fireEvent(obj, evt) {
    var fireOnThis = obj;

    if (document.createEvent) {
        // alert("FF");
        var evtObj = document.createEvent('MouseEvents');
        evtObj.initEvent(evt, true, false);
        fireOnThis.dispatchEvent(evtObj);
    } 

    else if (document.createEventObject) {
        // alert("IE");
        var evtObj = document.createEventObject();
        fireOnThis.fireEvent('on'+evt, evtObj);
    }
}

Try this if you are still getting error (Use of Jquery + prototype) 如果仍然出现错误,请尝试此操作(使用Jquery +原型)

function fireEvent(element,event){  
if (document.createEventObject){
// dispatch for IE
try {
      var evt = document.createEventObject();
      jQuery(element).change();
      return element.fireEvent('on'+event,evt);
} catch(e) { }                  
}
else{
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
}

If you want to simulate clicks only for links , you can use this: 如果您只想模拟链接的点击次数,可以使用以下命令:

function clickLink(id){
location.href=document.getElementById(id).href;
}

We found a simpler way to simulate right click (tested in IE8). 我们发现了一种更简单的模拟右键单击的方法(在IE8中测试)。 Use a double click or two single clicks, then right-click using Shift-F10. 使用双击或两次单击,然后使用Shift-F10右键单击。 I don't know exactly why this works, but it does. 我不确切知道为什么会这样,但确实如此。 In the sample code below, we use the Selenium method to click twice, then use the IE Driver to find the WebElement and send the key sequence Shift-F10, which emulates the right-mouse button click. 在下面的示例代码中,我们使用Selenium方法单击两次,然后使用IE驱动程序查找WebElement并发送键序列Shift-F10,它模拟鼠标右键单击。 We use this to testing GWT based web apps. 我们使用它来测试基于GWT的Web应用程序。 One place this did not work as well was in a tree control where the context menu was set to appear at the mouse's coordinates. 其中一个地方不起作用的是树控件,其中上下文菜单设置为出现在鼠标坐标处。 Often, the mouse coordinates were negative, so right-clicking the menu item did not cause the child menu options to get displayed. 通常,鼠标坐标为负,因此右键单击菜单项不会导致显示子菜单选项。 To handle this case, we added a bit of code to the control so that if the mouse coordinates were negative, the context menu appears at a 0,0. 为了处理这种情况,我们向控件添加了一些代码,这样如果鼠标坐标为负,则上下文菜单显示为0,0。

selenium.click("//td[@role='menuitem' and contains(text(), 'Add')]");
selenium.click("//td[@role='menuitem' and contains(text(), 'Add')]");
new InternetExplorerDriver().findElement(By.xpath("//td[@role='menuitem' and contains(text(), 'Add')]")).sendKeys(Keys.SHIFT, Keys.F10);

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

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