为什么selenium通过Xpath定位元素在IE中失败,但在firefox中却可以正常工作?

发布于 2024-10-07 01:57:27 字数 3031 浏览 4 评论 0原文

我的 selenium 自动化测试脚本在 Firefox 中运行,并且运行良好。 现在我想在IE中运行脚本,似乎在在GUI中定位元素的步骤失败。似乎selenium无法在GUI中找到该元素,而firefox可以轻松找到它。

String servicenameidtext = "//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td";
Selenium.getText(servicenameidtext );

事实上,脚本是用selenium-IDE录制的,我检查了Xpath,我认为元素没有问题。 但是为什么selenium在IE中这一步失败了?

有人能告诉我原因吗?在IE中运行测试时出现问题如何解决? 多谢!

我想通过以下方式从 GUI 中定位元素“alarmlm”

String servicenameidtext = "//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td" ;

P.S:  The html of the part:
<tbody><tr class="form-title"><td colspan="3"><a href="javascript:void(0);" onclick="javascript:ChangeVisibility( 'release_hostsSoftwareInformation' ); return false;"><img src="img/minus.png" title="-" alt="-"></a>&nbsp;<b>Software Information</b>
</td></tr><tr><td><div id="release_hostsSoftwareInformation_internal">
<table id="release_hostsProcesses_list" border="0" width="100%" class="Processes_list"><tbody>
<tr class="title-column"><td><b>Service Name</b></td><td><b>Summary</b></td><td><b>Description</b></td><td><b> Version </b></td><td><b>Release</b></td><td colspan="2"><b>Installation Date</b></td></tr><tr><td>alarmlm</td><td>mpm</td><td>MultiPlatformManagement tools</td><td>4.0.12</td><td>5</td><td>Wed 10 Nov 2010 04:23:52 AM CST</td></tr><tr><td>annlabclient</td><td>Annlab Client</td><td>Annlab Client</td><td>5.0.13</td><td>23</td><td>Wed 10 Nov 2010 04:23:57 AM CST</td></tr><tr><td>annlabserver</td><td>Annlab Server</td><td>Annlab Server</td><td>5.0.13</td><td>23</td><td>Wed 10 Nov 2010 04:23:57 AM CST</td></tr><tr><td>recoveryalarmlm</td><td>mpm</td><td>MultiPlatformManagement tools</td><td>4.0.12</td><td>5</td><td>Wed 10 Nov 2010 04:23:52 AM CST</td></tr><tr><td>recoverystatlm</td><td>mpm</td><td>MultiPlatformManagement tools</td><td>4.0.12</td><td>5</td><td>Wed 10 Nov 2010 04:23:52 AM CST</td></tr><tr><td>mrfctrl</td><td>MRF Controller</td><td>mrfctrl - including sipproxySubsystem</td><td>1.4.0.5</td><td>1</td><td>Wed 10 Nov 2010 04:24:00 AM CST</td></tr></tbody></table></div></td></tr></tbody>

谢谢大家,问题已经解决了。原因是:firefox和IE中的html不一样。奇怪。

I have my selenium automation test scripts run in firefox, and it works well.
Now I want to run the scripts in IE, it seems it failed on the step of locating an element in GUI. It seems selenium can't find the element in GUI while firefox can easily find it.

String servicenameidtext = "//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td";
Selenium.getText(servicenameidtext );

In fact, the scripts is recorded by selenium-IDE, and I have check about the Xpath, I think there is no problem of the element. But why selenium failed on this step in IE?

Could anyone tell me the reason? And how to solve the problem when running test in IE?
Thanks a lot!

I want to locate the element "alarmlm" from the GUI by

String servicenameidtext = "//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td";

P.S:  The html of the part:
<tbody><tr class="form-title"><td colspan="3"><a href="javascript:void(0);" onclick="javascript:ChangeVisibility( 'release_hostsSoftwareInformation' ); return false;"><img src="img/minus.png" title="-" alt="-"></a> <b>Software Information</b>
</td></tr><tr><td><div id="release_hostsSoftwareInformation_internal">
<table id="release_hostsProcesses_list" border="0" width="100%" class="Processes_list"><tbody>
<tr class="title-column"><td><b>Service Name</b></td><td><b>Summary</b></td><td><b>Description</b></td><td><b> Version </b></td><td><b>Release</b></td><td colspan="2"><b>Installation Date</b></td></tr><tr><td>alarmlm</td><td>mpm</td><td>MultiPlatformManagement tools</td><td>4.0.12</td><td>5</td><td>Wed 10 Nov 2010 04:23:52 AM CST</td></tr><tr><td>annlabclient</td><td>Annlab Client</td><td>Annlab Client</td><td>5.0.13</td><td>23</td><td>Wed 10 Nov 2010 04:23:57 AM CST</td></tr><tr><td>annlabserver</td><td>Annlab Server</td><td>Annlab Server</td><td>5.0.13</td><td>23</td><td>Wed 10 Nov 2010 04:23:57 AM CST</td></tr><tr><td>recoveryalarmlm</td><td>mpm</td><td>MultiPlatformManagement tools</td><td>4.0.12</td><td>5</td><td>Wed 10 Nov 2010 04:23:52 AM CST</td></tr><tr><td>recoverystatlm</td><td>mpm</td><td>MultiPlatformManagement tools</td><td>4.0.12</td><td>5</td><td>Wed 10 Nov 2010 04:23:52 AM CST</td></tr><tr><td>mrfctrl</td><td>MRF Controller</td><td>mrfctrl - including sipproxySubsystem</td><td>1.4.0.5</td><td>1</td><td>Wed 10 Nov 2010 04:24:00 AM CST</td></tr></tbody></table></div></td></tr></tbody>

Thanks to all, the problem is solved. The cause is: the html in firefox and IE are not the same. Strange.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮萍、无处依 2024-10-14 01:57:27

也许是由于默认名称空间?

错误解释位于您未向我们展示的数据中 - 提供 (x)Html 文本怎么样?

更新

现在OP已经提供了源(x)Html文档。提供的 XPath 表达式:

//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td

不会仅仅因为 td 永远不是另一个 td 的子级而选择所需的元素,但这正是这个 XPath 表达式正在寻找——它以以下形式结尾:

td[1]/td

Maybe, due to a default namespace?

The error explanation is in the data you haven't shown us -- what about providing the (x)Html text?

UPDATE:

Now the OP has provided the source (x)Html document. The provided XPath expression:

//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td

doesn't select the required element simply because a td is never a child of another td, but this is exactly what this XPath expression is looking for -- it ends with:

td[1]/td
剩余の解释 2024-10-14 01:57:27

您可以尝试通过将 /text() 附加到 xpath 表达式来直接在 IE 中检查文本值是什么。也许它找到了空字符串?

String servicenameidtext = "//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td/text()";
alert("found:"  + servicenameidtext);

我的两分钱,

you could try to check out what the text value is directly in IE by appending /text() to the xpath expression. Maybe it finds the empty string?

String servicenameidtext = "//table[@id='release_hostsProcesses_list']/tbody/tr[2]/td[1]/td/text()";
alert("found:"  + servicenameidtext);

My two cents,

不必你懂 2024-10-14 01:57:27

尝试在 id 字段中使用所有小写字符:

String servicenameidtext = "//table[@id='release_hostsprocesses_list']/tbody/tr[2]/td[1]/td";
Selenium.getText(servicenameidtext );

应该在 FF 和 IE 中工作

Try to use all lowercase characters in id field:

String servicenameidtext = "//table[@id='release_hostsprocesses_list']/tbody/tr[2]/td[1]/td";
Selenium.getText(servicenameidtext );

Should work in both FF and IE

血之狂魔 2024-10-14 01:57:27

在这种情况下,我们可以做一件事

:检查 Internet Explorer IE 开发人员工具的元素,对于 FireFox Firebug,

我们可以比较这两种检查 从对象的 IE 开发人员工具中派生 XPath/CSS。

使用并检查它是否在 IE 和 FF 中都工作

如果工作正常否则有条件我们可以根据浏览器使用相应的 XPath。

In this Case we can do 1 thing

To Inspect Elements for Internet Explorer IE Developer Tools is available and for FireFox Firebug

We can compare both Inspections Derive the XPath/CSS from IE Developer Tools for the Object.

Use and Check is it working in both IE and FF

If it works Fine Otherwise Conditionally we can use the corresponding XPath According to Browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文