在C#中被发现部分ID元素硒 [英] Finding an element by partial id with Selenium in C#

查看:107
本文介绍了在C#中被发现部分ID元素硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个动态生成的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆