Selenium如何激活在单击时返回false的元素,并且sendkeys不起作用

时间:2018-01-06 18:24:59

标签: javascript c# css selenium

我已阅读了有关此内容的所有帖子并尝试了所有解决方案以及来自selenium HQ的一些解决方案但我仍然得到了相同的结果。点击事件触发或发送键去了没有错误但是应该出现的弹出窗口没有。 网站代码First Div是后续Div的父母:

<div id="27c16ba3-04f0-e711-8103-3863bb35cc58_userName" class="userName">
<a href="#" onclick="Wall.Control.Utils.WindowUtils.openObject(8,'b0b26cd5-1d84-e711-8109-e0071b66cfb1');
    return false;"
    title="Consultant">Consultant
</a>
</div> 
<div class="postActionsWrapper">
<a id="likeaction_27c16ba3-04f0-e711-8103-3863bb35cc58" 
    href="#" class="textAction " 
    title="Like this post" 
    onmouseup="dispatchWallCommand(this, 'likeaction', $P_CRM(this).tmplItem().data);
    return false;
    " onclick="return false;
    " onkeydown="var keycode = (window.event ? event.keyCode : event.which);
    if(keycode == 13 || keycode == 32){dispatchWallCommand(this, 'likeaction', $P_CRM(this).tmplItem().data); 
    return false;}"> LIKE 
</a>
<span class="separatorAction">|</span>
<a id="commentaction_27c16ba3-04f0-e711-8103-3863bb35cc58" 
    href="#" class="textAction " 
    title="Comment on this post" 
    onmouseup="dispatchWallCommand(this, 'commentaction', $P_CRM(this).tmplItem().data); 
    return false;
    " onclick="return false;
    " onkeydown="var keycode = (window.event ? event.keyCode : event.which);
    if(keycode == 13 || keycode == 32){dispatchWallCommand(this, 'commentaction', $P_CRM(this).tmplItem().data); 
    return false;}"> REPLY          
</a>
<a id="deleteaction_27c16ba3-04f0-e711-8103-3863bb35cc58" 
    href="#" 
    class="imageAction lastAction" 
    title="Delete this post" 
    onmouseup="dispatchWallCommand(this, 'deleteaction', $P_CRM(this).tmplItem().data); 
    return false;
    " onclick="return false;
    " onkeydown="var keycode = (window.event ? event.keyCode : event.which); 
    if(keycode == 13 || keycode == 32){dispatchWallCommand(this, 'deleteaction', $P_CRM(this).tmplItem().data); 
    return false;}">            
    <img src="/{636500878720000136}/WebResources//msdyn_/Images/actions_delete.png?ver=0" alt="Delete this post" border="0">              
</a>
</div>

以下是我试图让它发挥作用的一些事情。

public static void HoverOverAndClick(IWebDriver browser, By by)
{
        Actions action = new Actions(browser);
        IWebElement invisibleElement = browser.FindElement(by);
        action.MoveToElement(invisibleElement).Perform();
        /*
        if(IsTheElementVisible(invisibleElement))
        { action.Click(invisibleElement); }
        else //The method below is not the recommended path but it was the only one I found that worked
        {
            String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
            ((IJavaScriptExecutor)browser).ExecuteScript(js, invisibleElement);
            action.SendKeys(Keys.Return);
            Thread.Sleep(1000);
        }*/
        //String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject){ arguments[0].fireEvent('onmouseover');}";
        String onClickScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('click',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject){ arguments[0].fireEvent('onclick');}";
        String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
        ((IJavaScriptExecutor)browser).ExecuteScript(js, invisibleElement);
       // Thread.Sleep(3000);
       // ((IJavaScriptExecutor)browser).ExecuteScript(mouseOverScript, invisibleElement);
        Thread.Sleep(2000);
        ((IJavaScriptExecutor)browser).ExecuteScript(onClickScript, invisibleElement);
    }//end hover over

有人猜测我接下来会尝试什么吗?因为我发生的一切都是试图直接进入DOM,这似乎浪费时间和精力。

1 个答案:

答案 0 :(得分:0)

经过大量的反复试验后,我发现以下代码适用于我正在测试的MS CRM应用程序。

       if(IsTheElementVisible(element))
        { action.Click(element); }
        else //The method below is not the recommended path but it was the only one I found that worked.  Directly manipulating the CSS is usually considered a no no.  I should have used Selenum to fake a mouse action but that did not work so...
        {
            browser.ExecuteScript($"document.getElementById('{element}').setAttribute('style', 'display: inline;')");
            element.Click();
        }

OHH,你需要在返回调用函数时进行线程休眠。由于某种原因,我不知道或理解,waituntil和线程睡眠在使元素可见并点击它以获取输入文本框的函数不会产生预期的结果。