How to use contains to get the attribute value in xpath

This is one my best finds so far and would like to share it with you. Here is an example that shows you how to use the xpath contains to build a xpath using part of the attribute value. This can be used when there are elements with the same id or text value.

Here is the source code of the page

Edit
Delete

Now I would like to build the xpath of the Edit link.
//a[contains(@href,’CustomerContacts’)][.=’Edit’]

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.

How to dynamically generate xpath by xpath axis

xpath axis

xpath axis image


We can dynamically generate xpath for a control on a web page by xpath axis.
For example, we want to locate “Add ” button by its corresponding category name, say “test” or “driving instructor”.

Then We can dynamically generate xpath for each add button using its corresponding category name by axis :

  1. //span[.=’test’] select a span element whose innertext is ‘test’
  2. //span[.=’test’]/parent::div select its parent div note of the specified span node
  3. //a[.=’Add’] select a button element whose innertext is’Add’
  4. //span[.=’test’]/parent::div/descendant::a[.=’Add’] select a button element whose innertext is’Add’ and who is a descendant of the div note //span[.=’test’]/parent::div

Then we can locate each button by its xpath “//span[.=’test’]/parent::div/descendant::a[.=’Add’] ” or “//span[.=’driving instructor’]/parent::div/descendant::a[.=’Add’]

How do I set or change the PATH system variable

Setting Path on Windows

For Windows XP:

  1. Start -> Control Panel -> System -> Advanced
  2. Click on Environment Variables, under System Variables, find PATH, and click on it.
  3. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
  4. Close the window.
  5. Reopen Command prompt window, and run your java code.

For Windows Vista:

  1. Right click “My Computer” icon
  2. Choose “Properties” from context menu
  3. Click “Advanced” tab (“Advanced system settings” link in Vista)
  4. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
  5. Reopen Command prompt window, and run your java code.

Setting Path on Solaris and Linux

To find out if the java executable is in your PATH, execute:

% java -version

This will print the version of the java executable, if it can find it.
If you get error java: Command not found. Then path is not properly set.

To find out which java executable the first one found in your PATH, execute:

% which java

Below are the steps to set the PATH permanently,

Note: We are here giving instructions for two most popular Shells on Linux and Solaris.

Please visit link below if you are using any other shells.

Path Setting Tutorial

For bash Shell:

  1. Edit the startup file (~/ .bashrc)
  2. Modify PATH variable:

    PATH=/usr/local/jdk1.6.0/bin

  3. export PATH
  4. Save and close the file
  5. Open new Terminal window
  6. Verify the PATH is set properly

    % java -version

For C Shell (csh):

  1. Edit startup file (~/ .cshrc)
  2. Set Path

    set path=(/usr/local/jdk1.6.0/bin )

  3. Save and Close the file
  4. Open new Terminal window
  5. Verify the PATH is set properly

    % java -version

Submitted By Pavandeep Puddupakkam

Selenium – Invalid xpath [3]: XPath parse error

I was getting this error : Invalid xpath [3]: XPath parse error when I used xpath xpath=id(‘**IIIIIIHHHHHHHHHHHH’)[@href]. The solution is xpath=//a[@id=’**IIIIIIHHHHHHHHHHHH‘][@href]. Here I used //a as this was the tag id for the element.

I was facing this issue when I running the script on IE. There was no issue with the script on FF 3.6.

Submitted by Pavandeep Puddupakkam