Find Element using a wildcard

4,910 views
Skip to first unread message

MichaelF

unread,
Dec 22, 2011, 9:45:44 AM12/22/11
to webdriver
I'm trying to find a Page Element using webdriver driving an IE test,
I would like to avoid XPath if I can because of IE's reputation using
XPath. When trying to capture the ID of the element I want I
discovered part of the ID changes on page load, what I would like is a
method to use to capture the ID like -
ctl00_m_g_770f76bb_3148_4806_90e9_efadff4b4462_ctl02_TextBoxComments

I tried using Regex.Match on ctl100<capture any
characters>TextBoxComments but this doesn't work on FindElement,
unless there is a cast to use for getting the Regex.Match into an
IWebElement.

Is there a way to do this? Even if its XPath, which I can try, I'd
like to be able to capture the element.

Luke Inman-Semerau

unread,
Dec 22, 2011, 1:11:42 PM12/22/11
to webd...@googlegroups.com
Here's what you can do with CSS:


anything marked as CSS3 might not work in IE8.

Selenium injects Sizzle for IE6 and 7 for CSS selectors (which are css3 + some). And IE9 has CSS3 support. It's just IE8 that can be a bugger.

To get an element's ID that starts with a value ("ctl100") -

By.cssSelector("*[id ^='ctl100']")


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Niloy SQA

unread,
Dec 23, 2011, 3:51:22 AM12/23/11
to webdriver
Hay Luke

I am meeting one issue with css sector . can you plz help
me on that ?

link: http://groups.google.com/group/webdriver/browse_thread/thread/331fb0903331837d

Even i tried the following ways:

//driver.findElement(By.xpath("//dd[text()='My
Documents']//")).click();
//driver.findElement(By.cssSelector("dl[data-title=My
Documents]>dt")).click();
driver.findElement(By.cssSelector("div:contains('My
Documents')")).click();

no on worked for me. am I doing any mistake?

Regards
Nil

darrell

unread,
Dec 23, 2011, 9:37:09 AM12/23/11
to webdriver
If you are going the CSS3 route, there is also "*[id*='ctl100']". This
will look for any element (first asterisk) whose id attribute has
'ctl100' anywhere in it. So <div id='sdsf_ctl100_blah'> would match.
This is different from CSS2's ~= which requires whitespace around the
string. There is also $= which is CSS3 for 'ends with'. So "*[id
$='blah']" would find the element whose id attribute ends with 'blah'.

When CSS3 becomes standardized it will more than double the available
selectors.

Luke Inman-Semerau

unread,
Dec 23, 2011, 10:30:50 AM12/23/11
to webd...@googlegroups.com
When CSS3 becomes standardized it will more than double the available
selectors.

It is standardized. You'll only have problems when you need to test on older browsers that don't support it and have a querySelector. Selenium injects Sizzle when a querySelector isn't available. Sizzle implements the CSS3 standard. So the only browsers I can think of that you'll have a problem with are IE8 and FF3.0 (if you're even testing on that one). So if you were to have problems with those you could inject sizzle yourself on them (or if your page uses jquery it's there already)

-Luke

darrell

unread,
Dec 28, 2011, 11:10:13 AM12/28/11
to webdriver
Thanks for the correction. Haven't been keeping up on my standards.
CSS 3.0 was standardized on September 29th, 2011. So any browser older
than that might have issues with CSS 3.0 selectors.

Darrell

On Dec 23, 10:30 am, Luke Inman-Semerau <luke.seme...@gmail.com>
wrote:
> > When CSS3 becomes standardized it will more than double the available
> > selectors.
>
> It is standardized. You'll only have problems when you need to test on older browsers that don't support it and have a querySelector. Selenium injects Sizzle when a querySelector isn't available. Sizzle implements the CSS3 standard. So the only browsers I can think of that you'll have a problem with are IE8 and FF3.0 (if you're even testing on that one). So if you were to have problems with those you could inject sizzle yourself on them (or if your page uses jquery it's there already)
>
> -Luke
>
Reply all
Reply to author
Forward
0 new messages