如何获取一个CSS选择器使用Selenium WebDriver? [英] How to get a CSS selector using Selenium WebDriver?

查看:182
本文介绍了如何获取一个CSS选择器使用Selenium WebDriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为横幅菜单中显示的元素创建动态元素贴图,包括为每个菜单显示的链接。
此映射将存储在 String [] [] 矩阵中,将用于不同的方法和测试。

I'm trying to create a dynamic "element map" of the elements displayed inside a banner menu including the link displayed for each menu. This map will be stored inside a String[][] matrix, which will be used for different methods and tests.

为此,我得到元素的 href 属性,并为菜单中显示的每个链接创建我自己的CSS选择器。这是我的代码创建CSS:

To do that I get the element's href attribute and create my own CSS selector for each link displayed in the menus. This is my the code to create the CSS:

CSS = (String)element.getAttribute("href").subSequence((element.getAttribute("href").length()-20), element.getAttribute("href").length());

CSS="a[href*='" + CSS +"']";

这很好,但我有一些链接运行一些JavaScript代码,我不能创建CSS选择器。

That works pretty well, but I have some links that run some JavaScript code and I cannot create the CSS selector for them.

有没有其他方法从WebElement获取CSS选择器?
使用Firebug我可以得到CSS路径。在具有WebElement的执行期间,我可以获得这个CSS路径吗?有任何其他建议吗?

Is there any onther way to get the CSS selector from a WebElement? Using Firebug I can get the CSS path. Could I get this CSS path during the execution having the WebElement? Any other suggestions?

推荐答案

答案是。您不能从WebElement中提取CSS选择器。

The answer is No. You cannot extract a CSS selector from a WebElement.

为什么?因为WebElements本身由 By 类找到。如果指定为 xpath ?它将如何填充CSS选择器?

Why? Because the WebElements themselves are found by the By class.. What if the By specified was xpath? How would it populate the CSS selector?

这篇关于如何获取一个CSS选择器使用Selenium WebDriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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