Selenium - 无法点击元素c#

时间:2018-02-02 16:22:06

标签: c# selenium testing

我有一个测试点击树的所有节点元素并验证新页面的标题。我可以让它点击除一个元素之外的每个元素。该元素存在,我可以用鼠标点击它,但我无法通过测试点击它,它也不会失败。任何帮助将不胜感激!

测试中遇到问题的部分(使用了一个我没有问题的元素来验证悬停功能是否正常工作):

IWebElement othernode = IWebDriverExtensions.FindElement(By.Id("node122")); // another element, this one functions properly
HandleMouseHoverEvent(othernode); // hover over functioning element - this is successful
IWebDriverExtensions.wait();

IWebElement findme = IWebDriverExtensions.FindElement(By.Id("node100015")); // element I want to click on
Console.WriteLine("displayed " + findme.Displayed); // check if wanted element is displayed - returns True
Console.WriteLine(findme.Text); // prints text successfully
HandleMouseHoverEvent(findme); // hover over the element I want to click on - this doesn't work, but does not error
IWebDriverExtensions.wait();
findme.Click(); // click on the element I want to click on - this doesn't work, but does not error
Console.WriteLine("clicked"); // this gets printed

输出:

displayed True
New Prospect & Assignment
clicked

以下是两个元素(node122有效,node100015没有):

<tr nodetype="child" nodesequence="122">
    <td></td>
    <td colspan="2"><span>&nbsp;&nbsp;</span><span id="node122" onclick="try{UpdateNodes(event, this, 'RedirectTab','Interest/Project','action.aspx?treestate=-14-&amp;PageId=50&amp;AppId=10&amp;idnumber=000&amp;AppHeaderName=Prospect+Tracking++-+Interest%2fProject&amp;SelectedNodeSequence=122','Default.aspx?treestate=-14-&amp;PageId=50&amp;AppId=10&amp;idnumber=000&amp;AppHeaderName=Prospect+Tracking++-+Interest%2fProject&amp;SelectedNodeSequence=122', 1)} catch(exp){ }" onmouseout="OnNodeMouseOut(event, this, 1)" onmouseover="OnNodeMouseOver(event, this)" class="menuchild" lvl="1">Interest/Project</span><span></span></td>
</tr>
<tr nodetype="child" nodesequence="100015">
    <td></td>
    <td colspan="2"><span>&nbsp;&nbsp;</span><span id="node100015" onclick="try{UpdateNodes(event, this, 'RedirectTab','New Prospect &amp; Assignment','action.aspx?treestate=-14-&amp;PageId=50&amp;AppId=80&amp;idnumber=000&amp;AppHeaderName=Prospect+Tracking++-+New+Prospect+%26+Assignment&amp;SelectedNodeSequence=100015','Default.aspx?treestate=-14-&amp;PageId=50&amp;AppId=80&amp;idnumber=000&amp;AppHeaderName=Prospect+Tracking++-+New+Prospect+%26+Assignment&amp;SelectedNodeSequence=100015', 1)} catch(exp){ }" onmouseout="OnNodeMouseOut(event, this, 1)" onmouseover="OnNodeMouseOver(event, this)" class="menuchild" lvl="1">New Prospect &amp; Assignment</span><span></span></td>
</tr>

1 个答案:

答案 0 :(得分:0)

确保检查中的元素具有单一匹配。

如果正常点击不提供任何例外情况,我们可以使用以下方法点击。

1. var button = document.getElementById("....");
   button.click(); 

2. Actions action = new Actions(_driver);  
  action.MoveToElement(driver.FindElement(By.Id(".....")));
  action.Click().Build().Perform();