简体   繁体   English

如何使用Selenium WebDriver获取CSS选择器?

[英]How to get a CSS selector using Selenium WebDriver?

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. 该映射将存储在String[][]矩阵内,该矩阵将用于不同的方法和测试。

To do that I get the element's href attribute and create my own CSS selector for each link displayed in the menus. 为此,我获取了元素的href属性,并为菜单中显示的每个链接创建了自己的CSS选择器。 This is my the code to create the CSS: 这是我创建CSS的代码:

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

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

That works pretty well, but I have some links that run some JavaScript code and I cannot create the CSS selector for them. 效果很好,但是我有一些运行一些JavaScript代码的链接,但我无法为其创建CSS选择器。

Is there any onther way to get the CSS selector from a WebElement? 有没有其他方法可以从WebElement获取CSS选择器? Using Firebug I can get the CSS path. 使用Firebug,我可以获得CSS路径。 Could I get this CSS path during the execution having the WebElement? 在执行具有WebElement的执行过程中,能否获得此CSS路径? Any other suggestions? 还有其他建议吗?

The answer is No . 答案是否定的 You cannot extract a CSS selector from a WebElement. 您不能从WebElement中提取CSS选择器。

Why? 为什么? Because the WebElements themselves are found by the By class.. What if the By specified was xpath ? 因为WebElement本身是由By类找到的。如果指定的Byxpath怎么办? How would it populate the CSS selector? 如何填充CSS选择器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM