简体   繁体   English

xpath可用于硒记录,但不能在Eclipse中运行

[英]xpath works in selenium recording, but does not run in Eclipse

I am trying to run a test in Eclipse that was previously recorded in Selenium. 我试图在以前在Selenium中记录的Eclipse中运行测试。 I was able to get it to run in Selenium by using an XPath. 我能够通过使用XPath使其在Selenium中运行。 However, in Eclipse I get an NoSuchElement error. 但是,在Eclipse中,我收到了NoSuchElement错误。 I am trying to click on a tab on the page, the problem is that the tabs are not listed as buttons, but they are actually in a table. 我试图单击页面上的一个选项卡,问题是这些选项卡未作为按钮列出,但实际上在表中。 Here is the line that I have in place now. 这是我现在已有的行。

driver.findElement(By.xpath("(//div[@onclick=\"parent.frames.contentFrame.location='/messaging/maintfList.jsp?clearUIPath=true&uiPathLabel=Forms'\"])")).click();

Here is what I am shown when I inspect the element. 这是检查元素时显示的内容。 All of the tabs are listed as menu-normal, the only difference is in the pathlabel. 所有选项卡均列为正常菜单,唯一的区别是路径标签。

div class="menu-normal" onclick="parent.frames.contentFrame.location='/messaging/maintfList.jsp?clearUIPath=true&uiPathLabel=Forms'"

I would try 我会尝试

driver.findElement(By.cssSelector("div[onclick='parent.frames.contentFrame.location='/messaging/maintfList.jsp?clearUIPath=true&uiPathLabel=Forms']")).click();

escaping anything that needs it in java. 在Java中转义任何需要它的东西。

xpath is a very inefficient way to find things, and there is almost always a better way of doing it. xpath是一种发现事物的非常低效的方法,几乎​​总是有一种更好的方法。 CssSelector in this case can find what you are looking for. 在这种情况下,CssSelector可以找到您想要的东西。

Edit: I've had a look into Java documentation, try this line instead 编辑:我看过Java文档,请尝试使用此行

driver.findElement(By.cssSelector("div[onclick=\"parent.frames.contentFrame.location='/messaging/maintfList.jsp?clearUIPath=true&uiPathLabel=Forms'\"]")).click();

You could also try something like: 您也可以尝试以下方法:

driver.findElement(By.xpath("//div[@class='menu-normal'][2]")).click();

//here [2] is the position number of menu-normal on the page. //这里[2]是页面上正常菜单的位置号。 Let us assume there are 4 tabs with same class and you want to click on 2nd one then you can use above code. 让我们假设有4个选项卡具有相同的类,并且您想单击第二个选项卡,则可以使用上面的代码。

If it was not helpful, please share your html code. 如果没有帮助,请分享您的HTML代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM