WatiN:无法两次单击元素 - “找不到元素”错误
我遇到了一个问题,我需要单击一个元素两次。第一次单击该元素时,它会展开页面上的树,第二次单击时会关闭该树。第一次点击该元素时没有问题。
这是我正在使用的 HTML 代码的一部分:(我单击的元素是“a”标签。)
<div id="uasMainForm:uasTabs:0:agencyTree-d-rt-c" name="c">
<div title="" class="iceTreeRow" id="uasMainForm:uasTabs:0:agencyTree-d-4" name="nd">
<a id="uasMainForm:uasTabs:0:agencyTree:4" onclick="document.forms['uasMainForm']['uasMainForm:uasMainForm:uasTabs:0:agencyTree_idtn'].value='4';iceSubmitPartial( document.forms['uasMainForm'], this,event); return false;" onblur="setFocus('');" href="javascript:;">
奇怪的是,如果我在运行第一次单击后立即运行第二次单击,它会完美地关闭树,但是如果我用 MessageBox 或其他代码将其分解,则在尝试执行第二次单击时会引发错误。
这是有效的代码:
agency = ie.ElementOfType<Div>(Find.ByText(agencyName));
agencyTab = agency.ElementWithTag("a", Find.ByIndex(0));
agencyTab.Click();
//MessageBox.Show("Hey!");
agencyTab.Click();
这是无效的代码:
agency = ie.ElementOfType<Div>(Find.ByText(agencyName));
agencyTab = agency.ElementWithTag("a", Find.ByIndex(0));
agencyTab.Click();
MessageBox.Show("Hey!");
agencyTab.Click();
上面的代码抛出错误: 无法找到匹配条件的元素标签: Index = 0
我也尝试过 Find.ById 而不是 ByIndex ,结果相同。任何人都可以阐明在分解两个单击事件时而不是在单击事件连续发生时可能导致此错误的原因吗?
I am running into an issue where I need to click on an element twice. The first time the element is clicked it expands a tree on the page, the second time closes the tree. There is no issue when clicking the element the first time.
Here is the part of the HTML code I am working with: (The element I am clicking is the 'a' tag.)
<div id="uasMainForm:uasTabs:0:agencyTree-d-rt-c" name="c">
<div title="" class="iceTreeRow" id="uasMainForm:uasTabs:0:agencyTree-d-4" name="nd">
<a id="uasMainForm:uasTabs:0:agencyTree:4" onclick="document.forms['uasMainForm']['uasMainForm:uasMainForm:uasTabs:0:agencyTree_idtn'].value='4';iceSubmitPartial( document.forms['uasMainForm'], this,event); return false;" onblur="setFocus('');" href="javascript:;">
The weird thing is, if I run the second click immediately after running the first it closes the tree perfectly, but if I break it up with a MessageBox or some other code it throws an error when trying to perform the second click.
Here is code that works:
agency = ie.ElementOfType<Div>(Find.ByText(agencyName));
agencyTab = agency.ElementWithTag("a", Find.ByIndex(0));
agencyTab.Click();
//MessageBox.Show("Hey!");
agencyTab.Click();
Here is the code that does not work:
agency = ie.ElementOfType<Div>(Find.ByText(agencyName));
agencyTab = agency.ElementWithTag("a", Find.ByIndex(0));
agencyTab.Click();
MessageBox.Show("Hey!");
agencyTab.Click();
The above code throws the error: Could not find A element tag matching criteria: Index = 0
I have also tried Find.ById instead of ByIndex with the same result. Can anyone shed some light on what could be causing this error when breaking up the two click events, but not when the click events happen back to back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然不确定为什么会出现这个问题,但我找到了解决方法。首先,我在执行第一次单击之前保存包含 Div 的 Id:
然后,当我想要执行第二次单击时,我使用之前保存的 Id 再次查找包含 Div:
I'm still not sure why this issue was occuring but I found a way around it. First I save the Id of the containing Div right before I do the first click:
Then, when I want to perform the second click, I use the Id I saved earlier to find the containing Div again: