C# webdriver - 如何从已知元素类名中发现 xpath?

发布于 2024-11-08 07:31:12 字数 1191 浏览 0 评论 0原文

我正在测试一个网站,该网站有一个“帐户”页面,该页面显示可用订阅级别的表格,其中列标题是不同类型的帐户,行列出了所有功能以及哪些功能在哪个级别可用。例如:

          fanboy       poweruser     neophyte     freeloader
Widgets   1,000        500           250          100
Doodads     750        350           100          100
Thingies    500        250           100           50
          [UPGRADE]    [UPGRADE]     [UPGRADE]    Your Plan  

这一切都是作为具有唯一类名的嵌套 div 元素完成的,因此通过相应的类名查找特定单元格非常简单。例如:

var yourPlan = driver.FindElement(By.ClassName("your_plan"));  

如果我已经知道的话,我还可以通过 XPath 找到特定的单元格。例如:

var yourPlan = driver.FindElement(By.XPath(".//div[@class='your_plan']"));  

但有时我需要通过类名来发现单元格的 xpath,而在 Webdriver (C#) 中似乎没有一种简单的方法可以做到这一点。以前有人这样做过吗?


EDIT: Apologies to all, for not being clearer about this: I'm not having any trouble finding an element by classname or other attribute (or even xpath, when I know it already). But what I need, is a way for webdriver to report the full xpath of what it found to me, once it's found said element via a means other than xpath.

希望有帮助。

任何帮助表示赞赏。 谢谢 格雷格

I am testing a website that has an "account" page that displays a table of available subscription levels, where the column headings are the different kinds of accounts, and the rows list all features, and which are available at what level. E.g.:

          fanboy       poweruser     neophyte     freeloader
Widgets   1,000        500           250          100
Doodads     750        350           100          100
Thingies    500        250           100           50
          [UPGRADE]    [UPGRADE]     [UPGRADE]    Your Plan  

This is all done as nested div elements with unique classnames, so finding specific cells by their corresponding classnames is a snap. E.g.:

var yourPlan = driver.FindElement(By.ClassName("your_plan"));  

I can also find specific cells by their XPath, if I already know it. E.g.:

var yourPlan = driver.FindElement(By.XPath(".//div[@class='your_plan']"));  

But there are times when I need to discover the xpath of the cell I've come to by way of classname, and there doesn't seem to be an easy way to do this in Webdriver (C#). Has anyone done this before?


EDIT: Apologies to all, for not being clearer about this: I'm not having any trouble finding an element by classname or other attribute (or even xpath, when I know it already). But what I need, is a way for webdriver to report the full xpath of what it found to me, once it's found said element via a means other than xpath.

Hope that helps.

Any help appreciated.
Thanks
Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鼻尖触碰 2024-11-15 07:31:12

Selenium 实际上没有任何方法可以为您提供此信息。虽然有一些工具可以为给定元素生成 XPath(例如 Firefox 的 Firebug 和 XPather 插件),但这不是 Selenium 提供的功能。我希望能为您提供更好的答案,但答案是否定的,Selenium 不会为您提供通过其他方式找到的元素的 XPath。

There's not really any way for Selenium to give you this information. While there are some tools that will generate the XPath for a given element (the Firebug and XPather plug-ins for Firefox come to mind), this is not a feature that Selenium provides. I wish I had a better answer for you, but the answer is no, Selenium won't provide you with the XPath to an element that you've found via some other means.

陌路终见情 2024-11-15 07:31:12

只需使用以下内容创建 XPath 表达式字符串:

"//div[@class='" + className + "']"

如果类名确实是唯一的,则此 XPath 表达式将仅找到该 div。如果您不知道该元素是 div 还是其他元素,请使用

"//*[@class='" + className + "']"

Just create the XPath expression string with something like:

"//div[@class='" + className + "']"

If the classnames are indeed unique, this XPath expression will find only that div. If you don't know whether the element is a div or something else, use

"//*[@class='" + className + "']"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文