![](/img/trans.png)
[英]How to make XPath select multiple table elements with identical id attributes?
[英]How to select specific elements with certain attributes using AND in XPath?
如果元素名稱為[x或y或z]並且具有已定義的屬性,則我希望選擇同一棵樹中的所有元素。 在PHP中工作的示例是:
xpath = "//*[name()='x' or name()='y' or name()='z'][descendant::w[@attrib][@attrib2='".$variable."']]";
因此,我試圖在python 2.7中編寫其等效代碼:
xpath = "[//*[//x or //y or //z][descendant::w[@attrib][@attrib2='".{0}."']]".format(variable)
但這對我不起作用。 其實我找不到為什么PHP代碼使用name()嗎? 我可以並且應該在python中做同樣的事情嗎?
我寧願使用self::foo
而不是name()='foo'
。 以下XPath應該等效於您的第一個XPath:
xpath = '''//*[self::x or self::y or self::z]
[descendant::w[@attrib and @attrib2='{0}']]
'''.format(variable)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.