在C#中被发现部分ID元素硒 [英] Finding an element by partial id with Selenium in C#
问题描述
我试图找到一个动态生成的ID的元素。字符串的最后一部分是恒定的(ReportViewer_fixedTable),这样我就可以用它来定位的元素。我曾尝试XPath中使用正则表达式:
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")]"));
和定位由CssSelector:
And locating by CssSelector:
targetElement = driver.FindElement(
By.CssSelector("table[id$='ReportViewer_fixedTable']"));
无论是作品。任何建议,将不胜感激。
Neither works. Any suggestions would be appreciated.
推荐答案
这是因为CSS选择需要修改你几乎没有...
That is because the css selector needs to be modified you were almost there...
driver.FindElement(By.CssSelector("table[id*='ReportViewer_fixedTable']"))`
从的 http://sauceio.com/index.php/2010/01/selenium-totw-css-selectors-in-selenium-demystified/ :
css=a[id^='id_prefix_']
与 ID
与文字 id_prefix _
开始的链接。
css=a[id$='_id_sufix']
与 ID
与文本结尾的链接 _id_sufix
。
A link with an id
that ends with the text _id_sufix
.
css=a[id*='id_pattern']
与 ID的链接
包含文本 id_pattern
您用它我假设是不是你应该使用(除非我看见你的HTML部分链接文本标识,这意味着试图呈现一个后缀你的HTML下一次)。 * =
是,虽然任何情况下可靠的。
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.
这篇关于在C#中被发现部分ID元素硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!