Is it possible to use a regular expression in a locator

1,579 views
Skip to first unread message

JohnMc

unread,
Aug 10, 2010, 9:21:24 PM8/10/10
to webdriver
Is it possible to use a regular expression in a locator

Eg. myElement = driver.findElements(By.id("/some regex/"));

Daniel Wagner-Hall

unread,
Aug 10, 2010, 10:24:53 PM8/10/10
to webd...@googlegroups.com
Not currently (though you can use xpath which has limited regex
support). If you would find it particularly useful, patches are
always welcome, and I can run you through how you might implement this
in any of the particular drivers.

On 10 August 2010 18:21, JohnMc <j.mcgu...@bigpond.com> wrote:
> Is it possible to use a regular expression in a locator
>
> Eg. myElement = driver.findElements(By.id("/some regex/"));
>

> --
> 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.
>
>

Aaron

unread,
Aug 11, 2010, 2:23:15 PM8/11/10
to webdriver
Daniel is right. XPath is very powerful... and can be easier than
RegEx to implement. Prior to WebDriver, I had never used XPath, and
now I depend on it heavily for certain situations. After just a few
minutes of reading you should be able to start building low-
maintenance xpath strings some elements on your page.

Good luck.

--
AF

sheetal singh

unread,
Dec 3, 2012, 2:25:12 AM12/3/12
to webd...@googlegroups.com, aaron...@gmail.com
Hi,

Do we now have regex implementation in webdrivers for findby ID

 myElement = driver.findElements(By.id("/some regex/"));

Regards
Sheetal

Krishnan Mahadevan

unread,
Dec 3, 2012, 3:36:30 AM12/3/12
to webd...@googlegroups.com
Cant you achieve the same through including regex patterns with By.xpath ?

You could use xpath functions such as begins-with, contains etc.,

Wouldnt that solve your purpose ?


Thanks & Regards
Krishnan Mahadevan


My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/7D7A65b82NQJ.

sheetal singh

unread,
Dec 4, 2012, 2:51:02 AM12/4/12
to webd...@googlegroups.com
Hi,

I havnt really looked into xpath as i thought it would be difficult to implement xpath in my case.

I am fetching all the iframe get created in page whose id start with "ad_". So it would be good if regex can be applied to ID rather xpath.


Regards
Sheetal

Amit Agarwal

unread,
Dec 4, 2012, 3:20:37 AM12/4/12
to webd...@googlegroups.com
Hi Sheetal,

Have you tried with CSS selector using { id^="ad_" } or Xpath //div[starts-with(@id,'ad')]


Regards,
Amit




To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/3CJAl2XQdc4J.

sheetal singh

unread,
Dec 4, 2012, 8:07:09 AM12/4/12
to webd...@googlegroups.com
Hi Amit,

Thanks for the reply..

I used xpath and it actually solved my problem..its gr8
List<WebElement> element=driver.findElements(By.xpath("//iframe[starts-with(@id,'ad_')]"));
                   
However i am getting illegal string exception when i try to use css and how can we use curly braket inside "". Moreover from where i can learn more such functions for xpath,css etc?
List<WebElement> element=driver.findElements(By.cssSelector("id^=ad_"));

Regards
Sheetal

Amit Agarwal

unread,
Dec 4, 2012, 8:12:20 AM12/4/12
to webd...@googlegroups.com
Hi Sheetal,

For Xpath related stuff, u can refer to 

I am still looking for good resource on CSS, and truly speaking, still trying to learn CSS stuff. :)

Regards,
Amit


To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/jtJo_TgS5YEJ.
Reply all
Reply to author
Forward
0 new messages