Finding an element by partial id with Selenium in C#

0
0

I am trying to locate an element with a dynamically generated id. The last part of the string is constant (“ReportViewer_fixedTable”), so I can use that to locate the element. I have tried to use regex in XPath:

targetElement = driver.FindElement(
By.XPath("//table[regx:match(@id, "ReportViewer_fixedTable")]"));

And locating by CssSelector:

targetElement = driver.FindElement(
By.CssSelector("table[id$='ReportViewer_fixedTable']"));

Neither works. Any suggestions would be appreciated.

  • You must to post comments
0
0

That is because the css selector needs to be modified you were almost there…

driver.FindElement(By.CssSelector("table[id*='ReportViewer_fixedTable']"))`

From http://sauceio.com/index.php/2010/01/selenium-totw-css-selectors-in-selenium-demystified/:

css=a[id^='id_prefix_']

A link with an id that starts with the text id_prefix_.

css=a[id$='_id_sufix']

A link with an id that ends with the text _id_sufix.

css=a[id*='id_pattern']

A link with an id that contains the text id_pattern.

You were using a suffix which I’m assuming was not the partial link text identifier you were supposed to be using (unless I saw your html, which means try showing your html next time). *= is reliable in any situation though.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website