9 Replies Last post: Jan 19, 2009 7:29 AM by Mahesh  
Mahesh Advanced 89 posts since
Mar 7, 2008
Currently Being Moderated

Jan 9, 2009 3:50 AM

Using regular expression and filters like contains and start-with in DOM locators

Is it possible to use any such filters?

 

I have a bunch of buttons with id starting with 'save' i.e 'save100', 'save193', 'save198' and then another set that starts with 'reset' i.e 'reset100', 'reset193', 'reset198' etc. The 3 digit postfix number is generated at runtime and could be anything.

I am using dom=document.getElementsByTagName('input')[1], dom=document.getElementsByTagName('input')[2],dom=document.getElementsByTagName('input')[3] to operate on them.

But i would much like to identify them on the basis of their partial ID combined with their instance. For example I would like an expression 'the element whose id starts with 'webButton' and is the 2nd instance of it's kind'. Is this possible with DOM. I am only interested in DOM or CSS locators as I have to run this IE. I know this could be done using XPATH easily but I can't use XPATH as it does not work in IE..

If I were using  QTP to click the 'save100'  button given it is the first save button on the page (order in html document) then i would use something like

Browser().Page().webButton("html id=save.*","index=0").click

 

Please note nth-child in CSS-selector does not mean instance.

Bill Expert 212 posts since
Oct 3, 2008
Currently Being Moderated
1. Jan 9, 2009 10:54 AM in response to: Mahesh
Re: Using regular expression and filters like contains and start-with in DOM locators

Since when does xpath not work in IE?

Bill Expert 212 posts since
Oct 3, 2008
Currently Being Moderated
3. Jan 10, 2009 3:14 AM in response to: Mahesh
Re: Using regular expression and filters like contains and start-with in DOM locators

I've have developed a massive selenium framework for my company which runs primarily on IE (it is an enterprise app) but rus on firefox also and is exclusively in xpath, with some very complex expressions, and there isnt a single xpath v1 function which doesnt work in IE thats meant to, including 'contains' and 'starts-with'. Infact, about 80% of our expressions uses a 'contains', so I don't know where you have got this idea it doesn't work, because it does.

 

This is on both the ajaxslt and the javascript-xpath (v0.1.11) libraries which I ported to a few months back and needed very little changes to any expressions.

gim_h Almighty 736 posts since
Oct 17, 2007
Currently Being Moderated
5. Jan 11, 2009 3:46 AM in response to: Mahesh
Re: Using regular expression and filters like contains and start-with in DOM locators

hi Mahesh,

   i think you too mush misunderstand with xpath. not all xpath with 'contains' or 'start-with' function doesn't work in IE, nor does xpath starts with "xpath=". IE just doesn't correctly parse some xpath expressions which are too complex or too special.

 

  speaking of  thread http://clearspace.openqa.org/thread/16584, i said "xpath=(//input[contains(@id,'ctl00_middleContent_CheckBox')])[2]" doesn't work in IE, that means in this xpath IE can't parse the index. it has nothing to do with "xpath=" prefix and "contains" function. actually, "xpath=//input[contains(@id,'ctl00_middleContent_CheckBox')]" can work without problem both in FF and IE.

 

  is there a workround? of course. several day ago i ran into a page written by gyrm http://stressfreetesting.com/selenium-wiki/doku.php?id=locator_optimization. it talked about using "descendant axis" instead of "//" prefix. that gives me inspiration, i think this could solve your problem, and then i make an experiment. finally my assumption is proved, it works both in IE and FF. here is the script:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head profile="http://selenium-ide.openqa.org/profiles/test-case">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="selenium.base" href="http://video.google.com/" />

<title>discendant</title>

</head>

<body>

<table cellpadding="1" cellspacing="1" border="1">

<thead>

<tr><td rowspan="1" colspan="3">discendant</td></tr>

</thead><tbody>

<tr>

     <td>open</td>

     <td>http://video.google.com/</td>

     <td></td>

</tr>

<tr>

     <td>click</td>

     <td>xpath=/descendant::div[contains(@id, 'hs_v')][6]</td>

     <td></td>

</tr>

</tbody></table>

</body>

</html>

Bill Expert 212 posts since
Oct 3, 2008
Currently Being Moderated
6. Jan 11, 2009 6:47 AM in response to: gim_h
Re: Using regular expression and filters like contains and start-with in DOM locators

Indeed, It will solve the problem, I suggested using decendant instead of //  here for the same IE index issue: http://clearspace.openqa.org/message/52588 Which worked fine for me on his example

 

To me at least the use of decendant is actually a much better way of writting the expression full stop.

 

I'm not sure if this is a ajaxslt quirk or not, so the original expression could well work on javascript-xpath anyway.

gim_h Almighty 736 posts since
Oct 17, 2007
Currently Being Moderated
8. Jan 11, 2009 8:24 PM in response to: Mahesh
Re: Using regular expression and filters like contains and start-with in DOM locators

hi Mahesh,

if you use "descendant axes", the parentheses is needless:

Xpath=/descendant::input[contains(@id,'ctl00_middleContent_CheckBox')][2]

More Like This

  • Retrieving data ...