Xpath Contains Possible Bug

I wanted to discuss an issue before I put this issue on Bug Reporting.

Here is the situation.

I declare my xpath under object repository as:
//*[contains(@id, ‘someId’)] [contains(@class,‘someClass’)] [not(contains(@style,‘display: none;’))]

I ran my testcase and it works such as:

After running few times, I have realized that this gives an error. Then I went back to my object and check the xpath. Katalon automatically trimmed by xpath to:
//*[contains(@id, ‘someId’)]

Is this how Katalon supposed to act? I assume not, but would like to hear what others think.

I don’t know, but if it does do that “auto-magically” it makes total sense. The id attribute is unique throughout the document (if it isn’t, you have an invalid document). That being the case, everything else in your xpath is surplus “noise” and not required.

But how about if the Id dynamically changes and I have to specify the other attributes to specific point?

Don’t specify it. Either way, it’s not a bug.

XPath expressions need to be surrounded by quotes - and since the expression you are trying to parse also contains a string literal, I would suggest you switch the literal to single apostrophe '. Also, unless you expect the root element to contain the text menu, you’ll need to be more specific about the element you are searching for. For example, the following xpath:

driver.findElement(By.xpath("//*[contains(text(), ‘menu’)]"));
Will find the li element with the text “menu” (note xpath is case-sensitive):

  • menu
If possible, be even more certain, e.g. if you know that it is a li element:

driver.findElement(By.xpath("//li[contains(text(), ‘menu’)]"));
Edit (OP put the actual html up)

This will find the DIV element:

driver.findElement(By.xpath("//DIV[@style[contains(., ‘m‌​enubar_menubutton.png’)]]"));
Note that xpath is case sensitive - so you’ll need to duplicate the SHOUTCASE tags.

1 Like