0

I have a button element in a webpage

<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" title="Close">
    <span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span> 
    <span class="ui-button-text">Close</span>
</button>

I am trying to locate it using this Xpath: "//button[@class = 'ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close']"

This button element appears only when I choose to view an item within the page. There are multiple items within the page; so I need to open one by one after closing the one already open.

I need to use the Xpath multiple times to do my operation

btnWorkItemClose = bla.elementByXpath("//button[@class = 'ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close']");
btnWorkItemClose.click();
backlogGrid.sendKeys(Keys.ARROW_DOWN);  
backlogGrid.sendKeys(Keys.ENTER);
doSomeFuntionWithTheWorkItem();
btnWorkItemClose = bla.elementByXpath("//button[@class = 'ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close']");
btnWorkItemClose.click();

The element is successfully located during the first instance but not in the second usage. I got the exception

"no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class = 'ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close']"}" ]

Note: When I used the Xpath in the webpage manually, it found the element accurately & I have tried different wait methods but didn't help either.

However, when I used Xpath "//button[@title= 'Close']", it worked alright both the instances.

Can you please advise what is happening here?

stasiaks
  • 1,268
  • 2
  • 14
  • 31

2 Answers2

0

I think there is no need to remove from the second click

btnWorkItemClose = bla.elementByXpath("//button[@class = 'ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close']");

I have tried locate only once it is working fine

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

For xpath, class is an attribute and value compares as a strings. This mean @class = 'class1 class2' not equal @class = 'class2 class1' and in your case probably is the problem. Here examples how you can find element more effective:
- css selector: button[title='Close']
- css selector: button.ui-dialog-titlebar-close.ui-widget, here you can add as much classes as you need
- xpath: .//button[contains(@class='ui-dialog-titlebar-close'] or if you need to use more classes and do not worry about sequence of them, here is an example:
.//button[contains(@class,'ui-dialog-titlebar-close') and contains(@class,'ui-widget')]

Second problem, if you want get inner element with xpath you have put . at the beginning, details(What is the difference between .// and //* in XPath?). In you case you searching button element in all DOM, not inside bla. Here correct way to find close button inside bla element using xpath:

btnWorkItemClose = bla.elementByXpath(".//button[contains(@class,'ui-dialog-titlebar-close') and contains(@class,'ui-widget')]");
Sers
  • 12,047
  • 2
  • 12
  • 31
  • I think I wasn't clear with the question. I am working on a webpage which has several items within. When you click on an item, it opens a small window within the page. I do some function with that item and then click on the 'x' close button on the top right which closes the item. Then I open the next item and do the same thing. The xml attribute of the 'x' button is the same every time an item is opened. The problem I am facing is that, the xpath using class attribute works fine the first time but not the second. But using the title attribute in the xpath works fine. – Aro Sebastine Sep 02 '18 at 02:35
  • @AroSebastine check my updated answer, hope it's clear enough. – Sers Sep 02 '18 at 07:05
  • But in my case, the class attribute didn't change when I used the xpath the second time. As I said earlier, I was able to find the element using the same xpath on both instances manually in chrome developer tools. Interestingly using the same class attribute as below works: close = bla.elementByXpath("//button[@title= 'Close']"); for ( WebElement e : bla.elementsByXpath("//button")) { if ( e.getAttribute("class").equals("ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close")) { close = e; break; } }close.click(); – Aro Sebastine Sep 02 '18 at 19:04
  • What you try to do here and also in your question really not clear. Share url and bigger part of your code – Sers Sep 02 '18 at 19:16