如何找到Selenium WebDriver的XPath? [英] How to find the XPath of Selenium WebDriver?
本文介绍了如何找到Selenium WebDriver的XPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以获取 web 元素的 XPATH?
I want to know if it's possible to get the XPATH of the web element?
test = driver.find_elements_by_css_selector('div.menu-listes ul.menu.menu-horizontal.menu_left li h3')
for t in test:
//i want to have the XPATH
我试过 t.location
但我只得到 {'y': 0, 'x': 0}
i tried t.location
but i get just {'y': 0, 'x': 0}
推荐答案
来自重复问题的答案 在 Python 中(未经测试):
The answer from the duplicate question in Python (untested):
test = driver.find_elements_by_css_selector('div.menu-listes ul.menu.menu-horizontal.menu_left li h3')
for t in test:
driver.execute_script("""gPt=function(c){
if(c.id!==''){
return'id("'+c.id+'")'
}
if(c===document.body){
return c.tagName
}
var a=0;
var e=c.parentNode.childNodes;
for(var b=0;b<e.length;b++){
var d=e[b];
if(d===c){
return gPt(c.parentNode)+'/'+c.tagName+'['+(a+1)+']'
}
if(d.nodeType===1&&d.tagName===c.tagName){
a++
}
}
};
return gPt(arguments[0]).toLowerCase();""", t)
这篇关于如何找到Selenium WebDriver的XPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文