Selenium RC 使用 XPath 时遇到问题

发布于 2024-08-05 07:17:08 字数 1870 浏览 4 评论 0原文

我使用 Selenium RC 和 Firefox 的 chrome 模式来自动化 Web 应用程序的测试用例。我正在使用 TestNG 框架和 Eclipse 用 Ja​​va 编写脚本。现在说到要点:

我在使用 Selenium RC 识别某些 XPath 时遇到问题。我使用 Firefox 的 XPath-Checker 扩展验证了我的 XPath,没有显示任何错误。然后我在 Selenium IDE 中进行尝试,以确保 XPath 能够被识别。甚至 IDE 也能识别该元素。但 Selenium RC 就是不认识它。我可以做些什么来纠正这个问题吗?

具体来说,我试图单击由以下给出的特定区域:

html/body/form/div[@id='someid1']/div[@class='someClass']/div[@id='someid2']/div[@id='someid3']/div[@id='someid4']/div[@title='titleOfTheElement']

然后我也尝试过:

//div[@title='titleOfTheElement']
xpath=//div[@title='Automated User']
xpath=/descendant::div[@title='Automated User']

仍然没有!

1)有人可以建议可能出了什么问题,或者 Selenium 是否已知存在 XPath 问题?

2)是否有任何插件(类似于 XPath 检查器)可以帮助我们以 Selenium RC 的方式看待事物?这样我们就可以确定 RC 是否能够识别 XPath。

谢谢,
Mugen

这是 Selenium 代码:​​

selenium.click("somelink");
selenium.waitForPageToLoad("30000");

boolean flag=false
  do{
    if (selenium.isTextPresent("Some text on the page which loads last"))
    {
      flag=true
    }
  }while(flag=false);


selenium.click("locator for area which is driving me crazy");

现在,在最后一步,如果我单击页面上的其他任何位置(意味着其他一些定位器),则单击将起作用。

该区域的 HTML 如下所示:

<div id="someid1" style="overflow: hidden;">
<div id="someid2" title="title1" class="someclass">title1</div>
<div id="someid3" title="title2" class="someclass">title2</div>
<div id="someid4" title="required title" class="someclass">required title</div>
<div id="someid5" title="title3" class="someclass">title3</div>
<div id="someid6" title="title4" class="someclass">title4</div>
<div id="someid7" title="title5" class="someclass">title5</div></div>

非常感谢您对此进行研究。 :-)

I'm using Selenium RC with chrome-mode for Firefox to automate test cases for a web application. I'm writing the scripts in Java using TestNG framework and Eclipse. Now to the main point:

I'm having problems with Selenium RC for recognizing certain XPaths. I validate my XPaths with XPath-Checker extension for Firefox which shows no error. I then try it out in Selenium IDE to make sure that XPath is being recognized. Even IDE recognizes the element. But its the Selenium RC just doesnt recognize it. Is there anything I can do to correct this?

Specifically, I'm trying to click on a particular area given by:

html/body/form/div[@id='someid1']/div[@class='someClass']/div[@id='someid2']/div[@id='someid3']/div[@id='someid4']/div[@title='titleOfTheElement']

Then I also tried:

//div[@title='titleOfTheElement']
xpath=//div[@title='Automated User']
xpath=/descendant::div[@title='Automated User']

Still nothing!

1) Can someone please suggest what could be wrong, or whether Selenium is known to have problems with XPath?

2) Isn't there any addon (similar to XPath checker) that helps us to see things the way Selenium RC sees? This way we could be sure whether RC is going to recognize the XPaths.

Thanks,
Mugen

Here is the Selenium code:

selenium.click("somelink");
selenium.waitForPageToLoad("30000");

boolean flag=false
  do{
    if (selenium.isTextPresent("Some text on the page which loads last"))
    {
      flag=true
    }
  }while(flag=false);


selenium.click("locator for area which is driving me crazy");

Now at the last step if I were to click anywhere else on the page (meaning some other locator) the click would work.

The HTML for the area looks like this:

<div id="someid1" style="overflow: hidden;">
<div id="someid2" title="title1" class="someclass">title1</div>
<div id="someid3" title="title2" class="someclass">title2</div>
<div id="someid4" title="required title" class="someclass">required title</div>
<div id="someid5" title="title3" class="someclass">title3</div>
<div id="someid6" title="title4" class="someclass">title4</div>
<div id="someid7" title="title5" class="someclass">title5</div></div>

Thanks a load for looking into this. :-)

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

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

发布评论

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

评论(5

呆橘 2024-08-12 07:17:08

我不确定使用带有 title 属性的 div 是否正确。没有其他属性可以用来定位元素吗?

无论如何,这是定位器的 CSS 版本,以防万一它能工作:

css=div[title='Automated User']

I'm not sure if it's correct to have a div with a title attribute. Isn't there another attribute you could use to locate the element?

Anyway, here's the css version of the locator, in case it works:

css=div[title='Automated User']
饮湿 2024-08-12 07:17:08

在 Firefox 上运行 Selenium 测试时,我们曾经遇到过 XPath 表达式的问题。

  • 您是否尝试过使用不同的浏览器运行相同的测试?
  • 我记得我们用星号 (*) 替换了所有元素名称,这很有帮助。即

    //*[@id='someid1']/*[@class='someClass']/*[@id='someid2']

We had once an issue with XPath expressions when running Selenium tests on Firefox.

  • Have you tried running the same tests with different browser?
  • I remember we replaced all element names with asterisk signs (*) and that helped. i.e.

    //*[@id='someid1']/*[@class='someClass']/*[@id='someid2']

荒人说梦 2024-08-12 07:17:08

从您的示例中,您应该能够使用目标元素的 ID:

selenium.click("id=someid4");

尽管我怀疑 ID 是动态生成的,并且您无法使用此方法。

我看不出以下 xpath 不起作用的任何原因:

selenium.click("//div[@title='required title']");

定位它的其他方法是:

selenium.click("css=div[title='required title']");
selenium.click("css=.someclass:nth-child(4)"); //must be 4th child of parent element

div 是您的实际目标元素吗?它响应点击事件吗?如果有一个子元素响应点击,那么您将需要定位它。另外,您可以尝试使用以下命令进行故障排除:

mouseDown
mouseUp
fireEvent

From your example, you should be able to use the ID of the target element:

selenium.click("id=someid4");

Although I suspect that the IDs are dynamically generated and you are unable to use this method.

I can't see any reason why the following xpath wouldn't work:

selenium.click("//div[@title='required title']");

Other ways to target it would be:

selenium.click("css=div[title='required title']");
selenium.click("css=.someclass:nth-child(4)"); //must be 4th child of parent element

Is the div your actual target element? Does it respond to a click event? If there is a child element that responds to the click then you will need to target that instead. Also, you could try troubleshooting with the following commands:

mouseDown
mouseUp
fireEvent
夏末染殇 2024-08-12 07:17:08

您确定页面已通过 RC 正确加载,即您是否看到浏览器打开并加载页面?

Are you sure that the page is loaded correctly with RC, ie do you see the browser open and the page load?

厌倦 2024-08-12 07:17:08

可能是您的脚本比网页快。所以请尝试等待一段时间。
就像你可以使用 waitForCondition("selenium.isElementPresent("ElementID")","20000");

因为 xpath.try 似乎没有磨损,这可能会对你有帮助。

Might be your script is faster than the web page .so try to wait for some time.
like you can use waitForCondition("selenium.isElementPresent("ElementID")","20000");

As seems nothg worng with the xpath.try this it might help you.

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