Xpath @role or contains(@class)

682 views
Skip to first unread message

Nortier David

unread,
May 19, 2022, 4:12:58 AM5/19/22
to seleniu...@googlegroups.com

Hello

 

In c#, I want to find elements that have either a given role, either a specific classname

 

I want to fins all alerts

 

The code :

  • this.Driver.FindElements(By.XPath("//div[@role='alert' or contains(@class='alert')]")))

 

The error :

  • invalid selector: Unable to locate an element with the xpath expression //div[@role='alert' or contains(@class='alert')] because of the following error:
  • SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@role='alert' or contains(@class='alert')]' is not a valid XPath expression.
  •   (Session info: chrome=101.0.4951.67)

 

 

What the fuck ? 😊

 

Thank you all !

 

 

  David

 

 


====== DISCLAIMER ======

https://www.cph.be/maildisclaimer

Amal a a

unread,
May 19, 2022, 4:24:03 AM5/19/22
to seleniu...@googlegroups.com
Trythis bro 
  • this.Driver.FindElements(By.XPath("//div[@role='alert' or contains(@class,'alert')]")))

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/93de7136b1fe4f7494bb9ca4ed090975%40cph.be.

Nortier David

unread,
May 19, 2022, 4:30:07 AM5/19/22
to seleniu...@googlegroups.com

But yes !!! 😊

Thanks

 

De : seleniu...@googlegroups.com <seleniu...@googlegroups.com> De la part de Amal a a
Envoyé : jeudi 19 mai 2022 10:23
À : seleniu...@googlegroups.com
Objet : Re: [selenium-users] Xpath @role or contains(@class)

Sambhav Dave

unread,
May 19, 2022, 6:19:59 AM5/19/22
to seleniu...@googlegroups.com
I think the correct xpath should be : 

("//div[@role='alert' or contains(@class, 'alert')]")))

Please try and let me know.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/93de7136b1fe4f7494bb9ca4ed090975%40cph.be.
--
Thanks.

Sambhav Dave
Bhilai Nagar 490006, Chhattisgarh IN
(Sent from iOS)

Nortier David

unread,
May 19, 2022, 6:22:13 AM5/19/22
to seleniu...@googlegroups.com

Yes, somoeone have found the mistake

Thanks

 

De : seleniu...@googlegroups.com <seleniu...@googlegroups.com> De la part de Sambhav Dave
Envoyé : jeudi 19 mai 2022 12:19


À : seleniu...@googlegroups.com
Objet : Re: [selenium-users] Xpath @role or contains(@class)

 

I think the correct xpath should be : 

Vishal Patil

unread,
May 19, 2022, 7:06:12 AM5/19/22
to seleniu...@googlegroups.com
Hi 
Can you please chhange = to ,

//div[@role='alert' or contains(@class,'alert')]
On Thu, 19 May 2022 at 1:42 PM, Nortier David <N...@cph.be> wrote:
--

Anuv4123

unread,
May 19, 2022, 8:02:56 AM5/19/22
to Selenium-Users
When using contains always you should use , instead of = sign. Use as 

  • this.Driver.FindElements(By.XPath("//div[@role='alert' or contains(@class,'alert')]")))

Thanks 



--

Scott Babcock

unread,
May 25, 2022, 1:54:05 AM5/25/22
to Selenium Users
One thing to be very careful with in XPath is the `contains` function. With the formulation shown in example in this thread, you have the potential for incorrect matches. Use this instead:
  • this.Driver.FindElements(By.XPath("//div[@role='alert' or contains(concat(' ',@class,' '),' alert ')]")))
The key here is that we're ensuring that we only match the "alert" class precisely. Without concatenating spaces to the ends of the class value and matching " alert ", we could match elements with class values in which "alert" appears anywhere (e.g. - "unalerted").

This is why I prefer CSS selectors. These locators are specifically designed to match class specifiers:
  • this.Driver.FindElements(By.CssSelector("div.alert, div[role='alert']")))
This CSS selector yields the same results, but is much more concise.

Nortier David

unread,
May 25, 2022, 3:48:58 AM5/25/22
to seleniu...@googlegroups.com

Nice ! Thanks

 

De : seleniu...@googlegroups.com <seleniu...@googlegroups.com> De la part de Scott Babcock
Envoyé : mercredi 25 mai 2022 07:54
À : Selenium Users <seleniu...@googlegroups.com>


Objet : Re: [selenium-users] Xpath @role or contains(@class)

 

One thing to be very careful with in XPath is the `contains` function. With the formulation shown in example in this thread, you have the potential for incorrect matches. Use this instead:

Reply all
Reply to author
Forward
0 new messages