简体   繁体   中英

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. I was able to get it to run in Selenium by using an XPath. However, in Eclipse I get an NoSuchElement error. 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.

xpath is a very inefficient way to find things, and there is almost always a better way of doing it. CssSelector in this case can find what you are looking for.

Edit: I've had a look into Java documentation, try this line instead

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. Let us assume there are 4 tabs with same class and you want to click on 2nd one then you can use above code.

If it was not helpful, please share your html code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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