# Xpath syntax error when I use \`contains\` for \`classname\` and \`text\` attributes

**URL:** <https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [February 22, 2022, 10:05am UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750 "2022-02-22T10:05:24Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 10:05am UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/1 "2022-02-22T10:05:24Z")

</div>

What I am trying to do is to get all `<checkBox>`es, which belong to a `<div>`, which `<div>`s `classname` contains the `string` “something” and it’s `text` contains the `string` of a `GlobalVariable`.

To achieve this I am trying something like this:

```auto
WebDriver driver = DriverFactory.getWebDriver()

List<WebElement> wElements = []

wElements = driver.findElements(By.xpath('//div[contains(@class, "something" and contains(text(), '+GlobalVariable.somethingElse+'))]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"]'))

for(WebElement element in wElements) {
	String elementText = element.getText()
	println(elementText)
}

```

And I get this syntax error:

```auto
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //div[contains(@class, "something" and contains(text(), Something Else))]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, "something" and contains(text(), Something Else))]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"]' is not a valid XPath expression.

```

Now, if I remove the part `and contains(text(), '+GlobalVariable.somethingElse+')` the test case ends successfully. So the syntax error should be here.

I have read [this](https://docs.katalon.com/katalon-studio/docs/detect_elements_xpath.html#deal-with-dynamic-elements-using-xpath) document and this [article](https://www.guru99.com/xpath-selenium.html), and I have try some things like to type `=` instead of `,` after `text()` or to replace `text(),` with `.,` or to make it like this:

```auto
'//div[contains(@class, "datagrid-row song")][contains(text(), '+GlobalVariable.somethingElse+')]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"]'

```

…but, nothing!!! I can’t understand what am I doing wrong here. If someone can see something…

Thank you for your time!!!

**─[EDIT 1]─────**

After I changed:  
`[contains(text(), '+GlobalVariable.somethingElse+')]`  
To:  
`[contains(text(), "'+GlobalVariable.somethingElse+'")]`  
…I am not getting any errors but neither a result!!! So, something should be wrong with the text attribute of this elements now. Because the text I type into `[contains(text(), '+GlobalVariable.somethingElse+')]` exists for sure…

**─[EDIT 2]─────**

To check if the outer div prints it’s text I try this:

```auto
WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> wElements = []
wElements = driver.findElements(By.xpath('//div[contains(@class, "datagrid-row song")]'))
for(WebElement element in wElements) {
	print(element.text)
}

```

And I get the text in three different lines like this!!!:

```auto
2022-02-22 13:34:34.681 DEBUG testcase.*** - 1: println(text)
18
La processione degli equinoziKeplero's House
05:59

```

Could this be the reason why it can’t find the text I am asking?

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 11:46am UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/2 "2022-02-22T11:46:00Z")

</div>

wrong:

> [@simos.sigma](#):
>
> ```auto
> wElements = 
> driver.findElements(By.xpath('//div[contains(@class, "something" and ....
> 
> ```

you need to change it to

```auto
wElements = 
  driver.findElements(By.xpath('//div[contains(@class, "something") and ...

```

one `)` is missing

---

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 11:51am UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/3 "2022-02-22T11:51:37Z")

</div>

So you mean something like this?

```auto
wElements = driver.findElements(By.xpath('//div[contains(@class, "datagrid-row song") and contains(text(), "'+GlobalVariable.artistName+'")]'))

```

Instead of this:

```auto
wElements = driver.findElements(By.xpath('//div[contains(@class, "datagrid-row song" and contains(text(), "'+GlobalVariable.artistName+'"))]'))

```

Because both working but don’t return results…

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:12pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/4 "2022-02-22T12:12:30Z")

</div>

> [@simos.sigma](#):
>
> So you mean something like this?  
> …  
> Instead of this:  
> …

Yes.

The latter one looks wrong to me.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:14pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/5 "2022-02-22T12:14:55Z")

</div>

> [@simos.sigma](#):
>
> Because both working but don’t return results

I do not understand what you mean by saying “working”.

I think you mean your code ran without “syntax error”.

If a XPath does not return a meaningful resutl, I would say, it is not working.

If the former statement does not return a meaningful result, it just means it has more problems to solve.

---

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 12:17pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/6 "2022-02-22T12:17:11Z")

</div>

Yes sorry this is what I mean… It runs without syntax error!!!

Can you please take a look at my 2nd edit and tell me what you think about this?

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:18pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/7 "2022-02-22T12:18:47Z")

</div>

No, I can not verify any XPath expression without target HTML source code exposed.

Only you can do verify XPath against your target HTML.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:22pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/8 "2022-02-22T12:22:25Z")

</div>

This xpath expression would fail

```auto
//div[contains(@class, "datagrid-row song") ...

```

You should change this to

```auto
//div[contains(@class, "datagrid-row") and contains(@class, "song") ...

```

Because your HTML source can be either of

```auto
<div class="detagridrow song">

```

or

```auto
<div class="song detagridrow">

```

---

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 12:27pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/9 "2022-02-22T12:27:29Z")

</div>

Oh… So when an element has more than one `class` names I should use it like this?

I changed it, no errors, no results!!!

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:29pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/10 "2022-02-22T12:29:39Z")

</div>

> [@simos.sigma](#):
>
> So when an element has more than one `class` names I should use it like this?

Yes, you should if each class name is significant to identify each HTML element. You can ignore insignificant class names, of course.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:30pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/11 "2022-02-22T12:30:25Z")

</div>

> [@simos.sigma](#):
>
> no results!!!

I don’t see why.

It’s up to you as you haven’t exposed your target HTML.

And in fact, I do not like to see it. Debugging locators is a task that could be resolved **only by TRY-ERROR-TRY AGAIN**.

I hope you do not ask others in the remote Forum for help about XPath/CSS Selectors that returns no result. That sort of question just annoys others who can not really help.

---

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 12:37pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/12 "2022-02-22T12:37:12Z")

</div>

Is it normal that returns it’s text in three different lines and not in one?

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:38pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/13 "2022-02-22T12:38:00Z")

</div>

> [@simos.sigma](#):
>
> in three different lines and not in one?

I do not see what you mean.

---

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 12:38pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/14 "2022-02-22T12:38:43Z")

</div>

Take a look at my 2nd edit please…

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:41pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/15 "2022-02-22T12:41:35Z")

</div>

> [@simos.sigma](#):
>
> And I get the text in three different lines like this!!!:
> 
> ```auto
> 2022-02-22 13:34:34.681 DEBUG testcase.*** - 1: println(text)
> 18
> La processione degli equinoziKeplero's House
> 05:59
> 
> ```

Possibly, the target HTML contains \n characters

- between `18` \n `La processione ...`
- between `House` \n `05`

You want to use [normalize-space()](https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/normalize-space) function of XPath.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 12:45pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/16 "2022-02-22T12:45:48Z")

</div>

You want to change this

```auto
//div[contains(@class, "datagrid-row song")]

```

to

```auto
normalize-space(//div[contains(@class, "datagrid-row") and contains(@class, "song")])

```

Then you will get a single line

```auto
18 La processione degli equinoziKeplero's House 05:59

```

Consecutive whitespaces (including \n) between words will be replaced to a single white space character by `normalize-space()` function. Leading and trailing whitespaces will be trimmed.

---

<div class="post-metadata">

**Author:** ![simos.sigma](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/simos.sigma/32/19248_2.png) [@simos.sigma](https://forum.katalon.com/u/simos.sigma)\
**Post date:** [February 22, 2022, 1:03pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/17 "2022-02-22T13:03:20Z")

</div>

It gives me error…

```auto
wElements = driver.findElements(By.xpath('normalize-space(//div[contains(@class, "datagrid-row") and contains(@class, "song")])'))

```

```auto
Reason:
org.openqa.selenium.InvalidSelectorException: Given xpath expression "normalize-space(//div[contains(@class, "datagrid-row") and contains(@class, "song")])" is invalid: TypeError: Document.evaluate: Result type mismatch

```

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 1:10pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/18 "2022-02-22T13:10:08Z")

</div>

Ah, I was wrong.

you can not use `normalize-space()` in the XPath inside `fineElement(By.xpath(...))`.

By.xpath(…) requires a XPath expression that retuns `Node-Set` type. But `normalize-space()` cast a NodeSet to a String.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2022, 1:12pm UTC](https://forum.katalon.com/t/xpath-syntax-error-when-i-use-contains-for-classname-and-text-attributes/62750/19 "2022-02-22T13:12:48Z")

</div>

You have got 3 `WebElement` instances. Therefore you got 3 lines.

I do not see any more reasonings as I do not see the target HTML.
