Alternative to using xpath contains text() in Selenium 

Here is an alternative to using contains text() in xpath command

script type="text/javascript"
try {
var pageTracker = _gat._getTracker("UA-222222-3");
pageTracker._trackPageview();
} catch(err) {}script

Now to get check the for the text UA-222222-3 in the javascript you can use the following syntax:

Assert.IsTrue(selenium.IsElementPresent("//script[@type='text/javascript'][.='UA-222222-3']);

contains text() is replaced with .=

How did this solution work for you? Do you have a better alternative? Feel free to share your thoughts in comments section below.


Google Webdriver Forum -The Online Community for Google Webdriver and Selenium RC Users and Professionals
2 responses to “Alternative to using xpath contains text() in Selenium”
  1. Yishai B says:

    Why is this better than using
    [contains(text(),'UA-222222-3')]
    or
    ?

    • Pavandeep Puddupakkam says:

      It all depends on what you are checking for. If you know the exact text then you can use text()=’222222-3′ but if you are looking for a pattern then its better to use contains(text(),’UA-222222-3′). If you are looking for the google analytics code you should make sure its the same as what is specified in the requirement.

Leave a Reply