ends-with in xpath is not working

5,802 views
Skip to first unread message

ARK Satyanarayana Raju

unread,
Jun 1, 2015, 5:58:47 AM6/1/15
to seleniu...@googlegroups.com
Hi,

I have written code to verify google logo is displayed or not in google homepage.

driver.findElement(By.xpath("//img[contains(@src, 'logo11w.png')]")).isDisplayed();

The above snippet is working fine. But if I use ends-with in xpath, It is throwing InvalidSelectorException.

I have tried below xpath:
driver.findElement(By.xpath("//img[ends-with(@src, 'logo11w.png')]")).isDisplayed();

Whats wrong with ends-with?

I am using FF36, Selenium-45.




Thanks,
Raju

sunny sachdeva

unread,
Jun 1, 2015, 6:54:09 AM6/1/15
to seleniu...@googlegroups.com
ends-with function is available for Xpath 2.0 and usually browsers lib uses xpath 1.0

You can either use contains or use substring function ...

Thanks
Sunny

tulsi.tester

unread,
Jun 2, 2015, 12:32:43 AM6/2/15
to seleniu...@googlegroups.com
Hi ARK,

As most of the web browsers support only xpath 1.0, we cannot use the endswith function which belongs to xpath 2.0 version. If you really looking for the same, here is the work around


<input name='firstname'>

I am trying to find whether it ends with 'name' or not

//input['name'=substring(@name,5,8)]



On Monday, June 1, 2015 at 3:28:47 PM UTC+5:30, ARK Satyanarayana Raju wrote:

A R K Satyanarayana Raju

unread,
Jun 2, 2015, 2:02:51 AM6/2/15
to seleniu...@googlegroups.com
Thanks to all,

I have achieved this using 

//div[substring(@title, string-length(@title) - 2) = 'gle']

--
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 post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/e40d1d0f-a3fc-4d55-9cd3-c66347d38a2e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
A R K Satyanarayana Raju

Rahul Shah

unread,
Jun 18, 2016, 10:35:24 PM6/18/16
to Selenium Users
what if name attribute in element do not have fix length?

Dmitri T

unread,
Jun 20, 2016, 2:05:01 AM6/20/16
to Selenium Users
For instance, you have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<something>
<label id="Ravi is a bad developer, he needs to be a barista"/>
<label id="Ajay is a bad tester, he needs to be a barista"/>
<label id="John is a good developer, he needs an appraisal"/>
</something>

If you need to match only those labels which end with "barista" you can use something like:

//label[substring(@id,string-length(@id) - string-length('barista')+1,string-length(@id))='barista']/@id

Demo:
















References:

Divya Harirajan

unread,
Dec 16, 2018, 5:56:19 AM12/16/18
to Selenium Users
Can you please explain this line?
Reply all
Reply to author
Forward
0 new messages