我需要一个 java 程序或一个 javascript 程序,它将动态地获取网页中 web 元素的所有 css 选择器(来自 DOM)......我已经为 gmail 登录页面使用了以下代码,但它不起作用。
公共字符串 javascript 函数(WebDriver 驱动程序){
String javaScript = "jQuery.fn.getPath = function ()"
+"{"
+ "if (this.length != 1)"
+ "throw 'Requires one element.';"
+ "var path,node = this;"
+ "while (node.length) {"
+ "var realNode = node[0], name = realNode.localName;"
+ "if (!name) break;"
+ "name = name.toLowerCase();"
+ "if (realNode.id) {" // As soon as an id is found, there's no need to specify more.
+ "return name + '#' + realNode.id + (path ? '>' + path : '');"
+ "}"
+ "else if (realNode.className) {"
+ "name += '.' + realNode.className.split(//s+/).join('.');"
+ "}"
+ " var parent = node.parent(), siblings = parent.children(name);"
+ "if (siblings.length > 1) name += ':eq(' + siblings.index(node) + ')';"
+ "path = name + (path ? '>' + path : '');"
+ "node = parent;"
+ "}"
+ "return path;"
+ "};";
System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"+(String) ((JavascriptExecutor) driver).executeScript(javaScript));
return (String) ((JavascriptExecutor) driver).executeScript(javaScript);